Hello coders! I hope y'all doing well. Today we're going to create a quick server using Express. Express is a Web Framework built upon Node.js.
Node.js is an amazing tool for building networking services and applications. Express builds on top of its features to provide easy-to-use functionality that satisfies the needs of the Web Server use-case. It's Open Source, free, easy to extend and very efficient.
Enough chit chat, let's get to it:
Step 1. Install NodeJs on your machine.
For this exercise you will need to have NodeJs installed on your machine. This allows your machine to run JavaScript code outside the browser.
Click on this link to download NodeJs.
Download the installation package and install it on your machine.
To ensure successful installation, run the following command.
node --version
or node -v
If the message > v18.13.0 is returned, then NodeJs is working just fine.
Step 2. Create a project folder and initiate the node modules and packages.
For this, go to your preferred directory and create a new folder. Then open the folder in your favorite code editor, preferably one that can access the command-line internally(Visual Studio Code).
Open the VSCode's command-line by clicking "Terminal on top-bar" or us "CTRL + ~".(For Windows the default command-line will be Command prompt and for Mac and Linux users the command-line will be the Terminal).
In the command-line, type the command,
npm init -y
This will create a new NodeJs project in your folder.
NPM stands for Node Package Manager. It's a standard package manager for JavaScript projects.
npm init: Creates a package.json file for your project. If you’re building an application from scratch, npm init will be one of the first commands you use to include key project information.
Step 3. Install Express.
To install express, run the command,
npm install express
Pretty straightforward right?😄😄
Step 4. Create the Server.
To do this you will need to create a new JavaScript file, server.js(the filename is your choice😉).
Inside the JavaScript file, write the following code.
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(5000, () => console.log('Server is open on port 5000'));
Go to the terminal and start up the server using,
node server.js
You can open the browser to port 5000 on localhost, "http://127.0.0.1:5000 or http://localhost:5000" and you should see the "Hello World!" message.
The above 4 lines of code do a lot behind the scenes.
First, we import the express package to the express value.
We instantiate an application by calling the express() method.
Once we have the application object, we tell it to listen for GET requests on
the / path, using the get() method.
If you're familiar with JS callback functions you might have noticed that both app.get() and app.listen() accept a callback function as a parameter. These functions are called when a request is started - and we need to handle it.
In the app.listen() method, there are two parameters. The first one is the server port on which the server is running.
You can use a direct value or first initialize a valuable as below.
const express = require('express');
const app = express();
const port = 5000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Server is running on port ${port}`));
Conclusion.
Now, there is a method for every HTTP verb: get(), post(), put(), delete() , patch() :
app.get('/', (req, res) => { /* / })
app.post('/', (req, res) => { / / })
app.put('/', (req, res) => { / / })
app.delete('/', (req, res) => { / / })
app.patch('/', (req, res) => { / */ })
We will look at each in the future. For now, PEACE coders✌️✌️!
Thank you for reading, feel free to insight, correct, inspire and feel inspired. See you in the next one😉.
Top comments (0)