How to Create a NodeJS Web Server with Express
NodeJS is continually growing in popularity, allowing adopters of the open-source JavaScript framework to create and maintain data-intensive, real-time web server applications.
Creating the NodeJS Web Server
To get started creating your NodeJS web server, you'll need to complete a few prerequisites and install a few dependencies, which this article will help walk you through.
First, you'll need to download NodeJS for Windows from the official website. The latest stable version as of this writing is 10.16.2. You can also download the current version containing the latest features, but it's best to only do so for testing purposes in development environments.
Once you've installed NodeJS, you can update NPM. NPM (Node Package Manager) is the default package manager for the JavaScript programming language which allows you to install and manage external modules for your environment. You can update to the latest version by opening a command prompt window and entering the following command:
npm update
Now that NodeJS and NPM are installed, you can create your first web application. First, you'll need to install the module dependencies using NPM in the command prompt, the Express framework and HTTP module. Without these dependencies, your application will not be able to function.
Enter the following commands in your command prompt window. Make sure that you're in your project's root folder so the modules can be included correctly.
npm install express
npm install http
If you're installing these dependencies in a Ubuntu or Mac environment, you may need to prefix both commands with sudo
to enable admin rights for download and installation.
The final step is to create an app.js
file to store your server code:
const express = require("express");
const app = express();
const http = require("http");
http.createServer({}, app).listen(3000, function () {
console.log("Hello World!");
});
This code creates an instance of Express for NodeJS as well as the module for setting up your HTTP instance to connect to the server. This app is listening on port 3000. You can set this port to whatever you need as long as that port is not already in use by another application or process.
To start your new NodeJS web server, enter the following command in your command prompt window:
node app
Once the web server has been started, the console will output the text "Hello World" as seen in the code.
NodeJS Web Server with HTTPS
Google highly recommends running your production environments in HTTPS, which requires obtaining an SSL certificate, or Secure Socket Layer certificate, from a certificate authority. There are many ways to purchase an SSL certificate. Many hosting providers offer free ones throughout different hosting packages. You can also get cheap SSL certificates through NameCheap, who I would highly recommend for smaller websites or blogs just starting out.
Once you've purchased your SSL certificate, you'll need to install it somewhere on your server. For testing purposes, just copy the certificate files to the root of your project to make them easily accessible.
We'll slightly modify the HTTP example above to handle HTTPS with our newly created SSL certificates.
const express = require("express");
const app = express();
const https = require("https");
const fs = require("fs");
const options = {
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem")
};
https.createServer(options, app).listen(3000, function () {
console.log("Hello World!");
});
There are a few changes to the existing code from the first example as well as a few additions and we'll go over all of them here. First, instead of accessing the http
module within NodeJS, we're now using https
. We're also including a new library called fs
, short for "File System", which we use in the next step to define the SSL certificate locations on the server with the options
constant object.
In this example, this object contains two variables, key
and cert
, which contain the file locations to the SSL certificate file and private key file. These are both required to enable usage of an HTTPS environment.
In the next and final step, we change our http
call to an https
call and the first argument in the createServer
method from the empty object we used in our first example to our new options
constant variable.
Save your changes and re-run the NodeJS server to allow browsing to your website in HTTPS.
Comments
There are no comments yet. Start the conversation!