Hey hey, I'm glad you're here! We'll be discussing how to set up a Node.js server locally using the Express web application framework. If you don't know much about Node.js, just understand this: Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. I hope that this tutorial helps you figure what exactly that means.
You can find the video tutorial here, and the code on GitHub.
First Thing's First
If you have not done this already, you'll need to install Node.js. This can be done at https://nodejs.org/. You should see a couple of download links on the front page, use the LTS version:
The setup is simple, just click next, next, next until it's all finished. You got this. :)
Server.js Setup
Adding Express
Go ahead and open a text editor, I use Sublime Text (it's free!) and create a new file. Let's call this file server.js
(this will be the only file we work with). Now, the first thing we want to do inside of this server.js
file is add Express to it. Express is a fast, unopinionated, minimalist web framework. We can do that by adding these two lines of code:
var express = require('express');
var app = express();
Adding a Route
The next thing we want to do is add a route to this server.js
file. Routing refers to the definition of application (server) end points, and how they respond to client (web browser) requests. We can do that by adding this piece of code:
// Our first route
app.get('/', function (req, res) {
res.send('Hello Dev!');
});
Listening to a Port
So now that we have an application framework and a route, the last thing we need to do is tell our server what port to listen to. We can do that by adding this code:
// Listen to port 5000
app.listen(5000, function () {
console.log('Dev app listening on port 5000!');
});
Final Product
Okay, cool! We've got our entire server file set up now. Its content looks like this:
var express = require('express');
var app = express();
// Our first route
app.get('/', function (req, res) {
res.send('Hello Dev!');
});
// Listen to port 5000
app.listen(5000, function () {
console.log('Dev app listening on port 5000!');
});
Running Server.js
Navigating to Server.js
We now have a server file, but how do we get it running? We first need to open the Node.js Command Prompt, you can find it by using your machine's search functionality. Now we need to navigate to our server file's directory by using these commands:
cd [directory name] // move into a directory
cd .. // move backward a directory
You can find more on command-line navigation here.
Defining the NPM Package
Can we just run the server file by itself? Well, no. But fortunately we can use NPM to set up everything else for us. NPM stands for "Node Package Manager". The first thing we want to do is run the command:
npm init
This will prompt for some information and produce a package.json
file. This package file defines what our application is. Once you've finished setting up the package file, you can move onto installing Express.
Installing Express
We know that our application wants to use the Express framework, and we added the code for it - but we still need to add it to our packages through NPM. In order to add Express, run the command:
npm install --save express
This tells our NPM to install the Express package, and save it to our project's package dependencies. If you open the package file, you will now see Express under the dependencies portion.
Running Server
And now we're here! We finally get to run the server. Inside of our server's directory, run the command:
node server.js
This tells Node.js to start the server! You'll see "Dev app listening on port 5000!". Head over to your web browser and go to http://localhost:5000. You should see "Hello Dev!" which is the content of our response from the first route. You can stop the server from running by using Ctrl+C
in the command prompt.
Adding Another Route
Okay, cool. We have our server running and a response for our index. But what if we want to add another route, how do we do that? We can add another route by simply adding another block of code:
// Our second route
app.get('/dev', function (req, res) {
res.send('Hello, you are now on the Dev route!');
});
Now, start your server again by running the command node server.js
in the command prompt. Navigate to http://localhost:5000/dev, and you'll see a different response than you get from the index route - it's the response from the /dev
route. Cool!
Review
You've now successfully set up a minimal server with multiple routes that runs locally. You did this by creating a Node.js application, adding the Express package dependency, and implementing routing. Congratulations! You can learn more about live deployment with Auto-Deploy a Node.js Server: Heroku + GitHub. If you'd like to learn more about routes, check out my article External Routes with Node.js.
Top comments (1)
Nice!