How to do 3-legged OAuth with GitHub, a general guide by example with Node.js

Try the live demo of the working tutorial.

3-legged authorization(OAuth) is the “Login with XXX” button you see everywhere. It’s a login protocol that lets the authentication provider authorize your application with the permission of the user. It redirects users to a new page, signs in, and approves the use of the user’s data. Some companies provide SDKs to make the process easier like Google and Facebook login, but if you learn the general process once, you can implement this for any API that requires 3-legged authentication.

Instead of implementing OAuth yourself, there are lots of frameworks out there like Passport.js or paid services like Auth0 for you to use in a production application. This post is intended to be a tutorial so you can learn it and not be too frustrated with the process.

Since the most popular APIs like Google and Facebook already provide custom SDKs, I’ll use GitHub’s OAuth API because it doesn’t have an officially recommended SDK. Their API doc page is very succinct, so if you’re already a seasoned Web developer, just head over to their site and read their guide instead.
Continue reading How to do 3-legged OAuth with GitHub, a general guide by example with Node.js

Using fetch() to Upload a File

File uploads are trickier than it seems. There are a few ways to do it on the client side. It really is just sending a POST/PUT request to the server you’re uploading to, but set the content type to multipart/form-data and the body is no longer strictly strings or JSON, but binary files, or any format you desire. One way to do it to use a form submission, which looks like this:

Continue reading Using fetch() to Upload a File

Simple File Upload with Express.js and Multer in Node.js

A couple of years ago I wrote a blog post on how to upload a file with formidable. I had the chance to write the upload function again, and found multer to be much easier to use. Here’s another beginner example.

This example saves your file into the file system.

Read the multer documentation to do different kinds of uploads.

Continue reading Simple File Upload with Express.js and Multer in Node.js

Parsing Forms with Multiple Submit Buttons in Node.js with Express 4

Here is a very simple, bare minimum example to demonstrate how Express 4 and body-parser module.
I’m using ejs as the rendering engine because it looks like html.

Setup

Continue reading Parsing Forms with Multiple Submit Buttons in Node.js with Express 4

Simple File Upload with Express.js and Formidable in Node.js

Here’s a very simple upload example with express 4+ and formidable. According to Express doc:

In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use a multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez.

So here’s an example combining the two.

The code is going to save the files you upload in disk under the folder /uploads in the root directory of the project. Continue reading Simple File Upload with Express.js and Formidable in Node.js

Send HTTP Requests To a Server with Node.js

It’s actually incredibly easy to send http/https requests to servers with Node.js. Node contains a bunch of native function for that, making it very easy to send REST requests. I’m going to demonstrate with Node’s own http.request(options[, callback]) method.

http-between-servers

HTTP/HTTPS requests in a very brief glance

The four most common HTTP requests are GET, POST, PUT, DELETE. The type of request determines the type of operation the request sender would like the server to perform.

Continue reading Send HTTP Requests To a Server with Node.js