2 Minute HTTP server using Express in Node.js

I covered setting up a simple HTTP server using Node.js previously, but that was using the package “http-server” for serving files.

Today I hit the point of needing to write server-side code, so my approach had to change.

Follow these steps to get up and running asap:

  1. Install Node.js if you haven’t already
  2. Open your shell of choice (Terminal, Bash, Command Prompt, etc.)
  3. “cd” to your Project folder
  4. If you don’t have a “package.json” file, run “npm init” and mash Enter
  5. Run “npm install express –save” (dash dash save, sorry for WordPress formatting)
  6. Copy and paste the code below into a new file “server.js”
  7. Run “node server.js”


// Read these comments first.
// Instructions for installing express here: https://expressjs.com/en/starter/installing.html
// cd into project directory
// run "npm init"
// mash enter until it's finished
// run "npm install express –save"
const express = require("express");
var host = "127.0.0.1";
var port = 8080;
var app = express();
app.use('/', express.static(__dirname + '/'));
app.listen(port, host);
console.log('Running server at http://localhost:' + port + '/');

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.