Now that we’ve seen how JWT based authentication works, let’s implement it using Node.

Creating the HTTP server

Let’s start by initializing the HTTP server with the required routes in the index.js file. We’ve used express as the server framework:

const express = require("express")
const bodyParser = require("body-parser")
const cookieParser = require("cookie-parser")

const { signIn, welcome, refresh…

Find out how to serve an `index.html` HTML page using Node.js with no dependencies

I had the need to serve an HTML page from a Node.js server, and this is the simplest code that does the trick:

const http = require('http')
const fs = require('fs')

const server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
fs.createReadStream('index.html').pipe(res)
})

server.listen(process.env.PORT || 3000)

No dependencies needed.

Add this to an app.js file, then create an index.html page, and run node app.js

Notice that the above code does not provide support for serving static assets, it will just serve the index.html page.

Harry Naruto

I have Master degree in science technology with web and mobile application development. Then, I hope have a lot of friends in my life for our business.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store