Hello everyone, today I am going to show how to create your first Express Server with Node Project. This is the first part of creating a MEAN Stack app.

Throughout this series, I will create a project with MongoDB, ExpressJS, AngularJS, and NodeJS. Also, show you how to host your project to heroku.com and add mongo database of mlab.com without any cost. I also show you how to use socket.io for real-time communication like chat.

Download & Install Necessary Software

First of all, you need to install node. So go to browser and download node for windows from nodejs.org.

Also, download vs code as the code editor, you can use others, it totally depends on you. Here vs code download link

After finish, your download installs both.
Now open the command prompt, if use MAC or Linux open terminal.
You can check node and npm version by below code.

npm -v
node -v

Create Node Project

Let switch to desktop and create a folder nodeDemo.

Now go inside the folder and then type npm init.

npm init.

Please check screenshot which will appear when you create node project;

screenshot of creating node project This will provide you some options to set up your project.

First, give a name to your package like node-demo. This name does not take any whitespace and special characters. It should be like URL string. Press enter.

node-demo

Set package version 0.0.1. Version contains 3 parts, first one for major change, the second one for minor change and last one for the bug fix. Press enter.

0.0.1

Add your project description you can keep it empty. Press enter.

my first node project

Then set your project entry point. Add a name of the file. Here I am using server.js. Press enter.

server.js

test command for testing your project with the different framework such as karma, jasmine. Keep it empty now. Press enter.

git repository for version control. Also, keep it empty. Press enter.

keywords, which help you to search your package on npm packages. Keep it empty. Press enter.

Add your name as author name. Press enter. Here I add someone as author
Your project setup is completed now. To open it on vs code type code . and press enter.
code .

If it does not work open visual studio code normally and click on file then select the open folder. Then choose your project folder and open it.

Only package.json file will be created by default. It may depend on node version. Here author name, version, package name description all come from your command inputs.

For vs code, you will see a terminal window. If it does not open then press Ctrl + `.

Setup Express Server

Now create a file same as your entry point file name for my case it is server.js. Now go to the terminal window to install npm package. Type npm install

npm install

you need express to set up the server. So install that by typing npm install –save express.

npm install --save express

–save is used to save this package at your package.json file. You will see a folder node_modules, which contains all your install packages.

Let’s do some code, import express reference which already installs. Here require is used to import any modules. Also, initialize express by typing app equal express() and import http module also.

Now take a port to start your node server. For node 3000 is common.

Now create your server using express. Then listen to the server with port 3000 and add a log inside the method. If the server starts successfully, you will see that log on the terminal.

Let’s set your first route for the request.  / indicate the root or entry point of your server. And here req contains client request information and res contains server response. Here I am sending back a normal string Hello Node!, which will be available for the client who sends the request or hit the address to the browser on which your server is running. In this case, it is http://localhost:3000.

Now go to terminal and type node server.js to start the server.

node server.js

See the terminal message, If it same as you print message then your server is started correctly. Now go to the browser and type http://localhost:3000 and press enter. You will see the Hello Node! the string which sends from the server.

You can start your server by npm start command.

For this, you need to add the additional command at scripts section on your package.json file like this “start”: “node server.js”.
Again here server.js is your server entry point file name.

Go to terminal and type npm start and press enter. You will see the message server started.
Now check in the browser it works same as before.

Source

Source Code link: github.com
Youtube Playlist:  MEAN Stack 
Mean Stack Tutorial List: Mean Stack(step by step)

Feel free to give your valuable feedback in comment section.


1 Comment

jaes · April 25, 2020 at 6:59 PM

Cool you compose, the data is great and intriguing, I’ll give you a connection to my site.
https://vert-shock.org

Leave a Reply

Your email address will not be published. Required fields are marked *