5.2. Creating a basic server with NodeJS: Setting up the development environment
Before we dive into creating a basic server with NodeJS, it's crucial to properly set up the development environment. This section will guide you step-by-step through that process.
Installing NodeJS
The first step is to install NodeJS on your machine. NodeJS is a JavaScript runtime environment that allows us to run JavaScript on the server. To install NodeJS, go to the official NodeJS website (https://nodejs.org) and download the latest version. Installation is quite simple and straightforward, just follow the on-screen instructions.
Verifying NodeJS installation
After installation, you can verify that NodeJS installed correctly by opening a terminal or command prompt and typing the following command: node -v
. This command should return the version of NodeJS you have installed.
Installing NPM
The next step is to install NPM (Node Package Manager). NPM is a package manager for NodeJS and is used to install third-party libraries and tools. Fortunately, NPM is automatically installed when you install NodeJS, so you don't have to worry about that.
Verifying NPM installation
To verify that NPM installed correctly, you can open a terminal or command prompt and enter the following command: npm -v
. This command should return the version of NPM you have installed.
Setting up the NodeJS project
Now that we have NodeJS and NPM installed, we can start configuring our NodeJS project. The first step is to create a new directory for our project. You can do this using the mkdir
command in your terminal or command prompt. For example: mkdir my-nodejs-project
.
Next, navigate to the project directory using the cd
command. For example: cd my-nodejs-project
.
Once inside the project directory, you must initialize a new NodeJS project using the npm init
command. This command will create a new file called package.json
in your project directory. The package.json
file is used to manage your project's dependencies and scripts.
Creating a basic server with NodeJS
Now that we have our development environment set up, we can start creating our basic server with NodeJS. To do this, we first need to create a new JavaScript file in our project directory. Let's call this file server.js
. You can create this file using the touch
command in your terminal or command prompt. For example: touch server.js
.
Next, open the server.js
file in your favorite text editor. Inside this file, we will import the http
module from NodeJS, which allows us to create an HTTP server. Here is the code to do this:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
This code creates a basic HTTP server that listens on port 3000 and responds with 'Hello World' to all requests. You can start the server using the command node server.js
in your terminal or command prompt.
Congratulations, you've just set up your development environment and created a basic server with NodeJS! In the next chapter, we'll explore how we can expand on this basic server to create complete RESTful APIs.