GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Check if a File Exists in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Working with the Node.js assert Library
  • Modify Authorized redirect_uris For Google OAuth
  • Fundamentals
  • Node
  • Vue
  • Webpack
  • Axios
  • Mongoose
  • Express
  • Lodash
  • npm

This view

  • Original page
  • New address

GeezerVison is showing masteringjs.io — HTTP Servers in Node.js

Mastering JS Logo
Mastering JS
Tutorials Newsletter eBooks Jobs
☰
Tutorials Newsletter eBooks Jobs
Tutorials / Node /

HTTP Servers in Node.js

Sep 30, 2020

Node.js has a built in http.Server class. Here's how you can start an HTTP server that responds to every request with the string 'Hello, World!':

const http = require('http');

// You usually don't call `new http.Server()`, the `http.createServer()`
// function creates a new `Server` instance for you.
const server = http.createServer((req, res) => res.end('Hello, World!'));

server instanceof http.Server; // true
await server.listen(3000);

Node.js' event loop based concurrency makes it easy to test HTTP servers. For example, you can start a server and then make an HTTP request to that server using the Axios HTTP library without any threads.

const http = require('http');

const server = http.createServer((req, res) => res.end('Hello, World!'));

server instanceof http.Server; // true
await server.listen(3000);

// Make an HTTP request to the server
const axios = require('axios');
const res = await axios.get('http://localhost:3000');
res.data; // 'Hello, World'

Versus Express

Most apps use an HTTP framework rather than using the http.Server class directly. The http.createServer() function just takes a single function as a parameter, so, if you use Node's HTTP server directly, you're responsible for implementing routing, HTTP body parsing, etc. Frameworks like Express take care of routing and body parsing, and provide patterns for organizing your code.

However, most frameworks use http.Server under the hood, and let you access the raw Node.js HTTP server. For example, Express' listen() function returns an instance of the http.Server class:

const express = require('express');
const app = express();

const server = app.listen(3000);
server instanceof require('http').Server; // true

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Node Tutorials

  • Getting Started with Oso Authorization in Node.js
  • Check if a File Exists in Node.js
  • How To Fix "__dirname is not defined" Error in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Working with the Node.js assert Library
  • Modify Authorized redirect_uris For Google OAuth
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.