NodeJS is an application development platform that allows the execution of JavaScript on the server side. It is built on top of Google Chrome's V8 JavaScript engine, offering a fast and efficient runtime environment. One of the key features of NodeJS is its ability to handle asynchronous I/O operations, making it ideal for building RESTful APIs and microservices.
NodeJS Installation
Installing NodeJS is a simple and straightforward process. It is available for a variety of operating systems including Windows, MacOS and Linux. To install NodeJS, you can download it directly from the official website at https://nodejs.org/. There you will find options to download the LTS (Long Term Support) version, which is the most stable and recommended for most users, or the current version, which contains the latest features and improvements, but may not be as stable as the LTS version.
Once you have downloaded the appropriate installer for your operating system, run it and follow the on-screen instructions. The default installation will install NodeJS, npm (node package manager), which is the package manager for NodeJS, and will also install Node.js command prompt, which is a command line interface for NodeJS.
Creating a basic server with NodeJS
With NodeJS installed, we can now start creating our basic server. For this, we first need to create a new JavaScript file. Let's call it 'server.js'. You can create this file in any text editor or IDE you prefer.
In 'server.js', let's start by importing the NodeJS HTTP module, which allows us to create an HTTP server. For this, we add the following line of code at the top of our file:
const http = require('http');
Next, let's use the 'createServer' function from the HTTP module to create our server. This function accepts a callback that is called whenever a request is made to our server. This callback takes two arguments: a request object, which contains information about the request made, and a response object, which we use to send a response to the client. Add the following code to your 'server.js' file:
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
Finally, we need to get our server to start listening for requests. For this, we use the 'listen' function of our server object, passing the port on which we want our server to listen. Add the following code to your 'server.js' file:
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Now, if you run your 'server.js' file using NodeJS, you should see the message 'Server running at http://127.0.0.1:3000/' displayed in the console, and if you open a browser and navigate to 'http://127.0.0.1:3000/', you should see 'Hello World' message displayed.
Congratulations! You've just created your first basic server with NodeJS. Although this server is very simple, it serves as a solid foundation for building more complex and powerful APIs.