Spin-up node.js server with express

This small project is good if you need to spin up a quick web server. You can run things like HTML pages, or even single-page node.js apps. I’ll show you where to get the required software, give you the code for the three files that we are going to manually create, and show you how to start it up through the command prompt or terminal. We’ll get started right after the jump.

If you don’t already have it installed, the first thing you will need to do is install node.js. Just choose the appropriate package depending on the setup of your machine, and install it.

Once you have node.js installed on your machine, open a terminal or command prompt and enter the following to check the node version:

node -v

This gives us two important pieces of information. If it responds with something like this, it tells you that node is, in fact, installed, also which node.js version you are working with.

With the terminal/command prompt open, navigate to the desired location, and create a new directory. You can name it whatever you would like. In the terminal, navigate to the directory that you just created and create three files:

  • server.js
  • package.json
  • index.html

In server.js paste the following code:

var http = require('http'); 
var express = require("express");
var app = express(); 
app.get('/', function(request, response){

var server = http.createServer(app);

What this file is doing is setting up an HTTP web server, and responding with index.html to any request on port 8000.

In package.json paste the following code:

 "name" : "testapp",
 "version" : "0.0.1",
 "description" : "Test App",
 "main" : "server.js",
 "author" : {
  "name" : "Your Name",
  "email" : "Your Email"

This sets up some properties in the scope of the application.

Finally in index.html paste the following code:

  <title>Hello, World! - Test App</title>
  Hello, World!

This is the HTML of the app, you can include the normal CSS/javascript files just as you normally would, and of course this is the basis of your content.

Now, all we have to do is go back to our terminal, and run express to build the rest of the framework by running the following code:

npm install express

This builds the framework and downloads the appropriate dependencies into the app’s directory.

Run the following code in the terminal to start the server:

node server.js

Now, open a new browser window and navigate to http://localhost:8000, you should see something like I have here:

So there you have it, a web server up and running, really anywhere you can install node.js, which is just about anywhere nowadays. This is just a starting off point, but it should show how easy it is to just begin to write your own apps, on a local machine, with no mess.

If you want to kill the web server just end the process in the terminal (usually by pressing ctrl+c), then close the terminal. All you have to do to turn it back on is navigate to the app’s directory in a terminal and run:

 node server.js

I hope this helps someone along the way, I know I found it hard the first time I tried to do this.

If this helps, don’t hesitate to leave a comment.

About the Author

Leave a Reply