• 10 hours
  • Medium

Free online content available in this course.



Got it!

Last updated on 2/21/22

Start a Node Server

What Is Node?

Before jumping in and writing code, let's take a moment to go over Node and Express.

Node is the runtime that allows you to write all your server-side tasks in JavaScript, like business logic, data persistence, and security. For example, it also adds functionalities that normal browser JavaScript doesn't have, like access to the local file system. 

Express is a framework that sits on top of Node and makes Node servers much easier to build and maintain, as you will see as you advance through this course. 

Initialize Your Project

Let's see in the next video how to initialize your project!

From within your  backend  directory, run the terminal command  npm init  to initialize the project. You can use the default options, or change them as you wish — however, your entry point should be  server.js , which you will create shortly.

Create a  server.js  file inside your back-end folder — this will contain your first Node server.

Start a Basic Server

Let's see step by step how to start a Basic Server!

To create a Node server in your server.js file, you will need the following code:

const http = require('http');

const server = http.createServer((req, res) => {
    res.end('This is my server response!');

server.listen(process.env.PORT || 3000);

Here, import the Node native HTTP package and use it to create a server by passing a function that will be executed every time a call is made to that server. That function receives the  request  and  response  objects as arguments. In this example, use the response's  end  method to send a string response back to the caller.

On the final line, set the server up to listen on either the port environment variable (potentially set by the platform to which you will deploy your server) or port 3000 for development.

Start this server by running  node server  from the command line. To check that it is sending the correct response, use a browser window to navigate to http://localhost:3000 (assuming you've completed the steps above).  

Alternatively, you can use a testing tool like Postman to make a GET request (or any other kind as the server does not differentiate for now!) to the same URL: http://localhost:3000 (again, assuming you've completed the steps above).

Install nodemon

In the next video, I'll show you how to install nodemon.

To make Node development simpler, you may wish to install nodemon by running

npm install -g nodemon

from the command line. Instead of using  node server  to spin up your server, you can use  nodemon server , which will watch your files for changes and restart the server, making sure it is always up to date.

In the next chapter, you'll add Express to the project to make building your API a walk in the park.

Let's Recap!

  • Node projects are initialized with  npm init  .

  • A basic Node server is created using the  http  package's  createServer  method. 

  • Nodemon is a package that watches your files for changes and automatically updates the running server, making your Node development experience much smoother.

Ready to create an Express application? That's what you'll learn in the next chapter! 

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement