Create a simple web server with Express(Node JS)


Hey guys! This is the fresh start of a brand new Express tutorials... Hope you have read my previous articles series es. If you are interested, this is the new one for you! What is Express ? Do you have any idea? Let me explain briefly.

What is Express?

I think you have already heard of the popular Run time Environment, Node JS. So Express is a Node JS framework that can be used simply to build web applications. 

It's fast and minimalist framework. Due to this tool, Coding using Node JS becomes very easy. There are lot of modules called node_modules available in Node JS that can be used with Express also.
For more details, visit the official site of Express framework.

I'm not going to explain you about its architecture. But I will show you the way to use this framework step by step..
Let's start!

What is our goal?
Usually NodeJS is used for server side configurations like creating a simple web server for us like Apache web server. It's the fundamental use of NodeJS. So we can do this using Express also with reduced steps compared to NodeJS.
prerequisites :
Node JS should be installed

Step 1 - Create a new project and initialize

Create a project with a name as you wish. I created as exmongo.  Then open project in VS Code or any text editor.
Navigate into the project with cmd.            cd exmongo
Node.js modules are a type of package that can be published to npm. To create a new module, we have to start by creating a package.json file. So we are going to create it.
Type this command on cmd now.
npm init
Then it will begin to create and you have to give some  details for this. After you entered this command, you will ask package name first. If you want to give a new one, type it. I recommend to keep it as default. Then hit enter..continue...
package name : exmongo
version : Keep as default and hit enter
description : Change if you want and hit enter
entry point : Type app.js and hit enter
test command , git repository, keywords : Keep as default and hit enter
author: Give your name - optional
license : Keep as default and hit enter


Finally give yes and terminate the process. Now there will be a new file called package.json with these details, in your project folder.

Step 2 - Install Express module into the project

We are going to use Express. So we use the below command to get the module. Open cmd and go into the project and type this.
npm install express --save

Output on cmd after installation :



Step 3 - Create the entry point

In our package.json file, we have give app.js as the entry point of the application. Therefore, now we are going to create a file called app.js in the project folder.
place this code in it.

console.log("Hello from Server");

Now we need to check the code is working or not. Open cmd and type this command.
node app.js
If there are no errors, cmd will output this..


We are ready to implement the server now!

Step 4 - Implement the server

Replace the code in app.js file with the following code.

// Load express module
const express = require('express');

// Initialize app
const app = express();

// Route for home
app.get('/', function (req, res) {
res.send('hello world')
});

// Start server with port 3000
app.listen(3000, function(){
console.log("Server started on localhost:3000");
});

Now we have completed the code for the server. Open cmd and type again  node app.js. Cmd will output this result..


Open the web browser and type localhost:3000 on address bar. Now you can see the root view of our project, since our server is up and running now!

We have fully implemented the web server now! This can be done without Express also..But few more steps.. Express is the easiest way I think.

So guys, this is the just beginning of Express tutorial. We will look deep into this framework in future articles. Practice makes you perfect!

Good Luck!



Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "Create a simple web server with Express(Node JS)"