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!
30 Comments
Thanx dr :D
ReplyDeleteThan you Anika! stay with mu blog..
ReplyDeleteBahkan sekarang kamu juga dapat melakukan transaksi dana pada situs situs tersebut tanpa harus memiliki rekening bank. Hal ini tentunya sangat memudahkan banyak orang
ReplyDeleteasikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna
bocoran sgp
data hk
You are amazing. The representaion of this article is well structured and the steps are easy to follow.
ReplyDeleteNode JS training in hyderabad
Thank you very much... :D
Delete
ReplyDeleteThanks for sharing valuable information. Your blogs were helpful.
Angular JS Online training
Angular JS training in hyderabad
Nice to hear. Thank you!
Deletevery nice blog thanks for the valuable information, i got an idea on how to Create a simple web server with Express
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
Thank you!
DeleteThat is so elegant and logical and clearly explained. Brilliantly goes through what could be a complex process and makes it obvious.
ReplyDeletePHP Training in Chennai | Certification | Online Training Course | Machine Learning Training in Chennai | Certification | Online Training Course | iOT Training in Chennai | Certification | Online Training Course | Blockchain Training in Chennai | Certification | Online Training Course | Open Stack Training in Chennai |
Certification | Online Training Course
Thank you! glad to hear :D
DeleteThank you!
ReplyDeleteThank you! it's a great pleasure :D
ReplyDeleteThank you!
ReplyDeletenice to hear! thank you!
ReplyDeleteHey, thanks for the blog article .Really looking forward to read more. Cool.
ReplyDeletepython training
angular js training
selenium trainings
thanks for the feedback
Deletethanks for the feedback
ReplyDeleteThis comment has been removed by the author.
ReplyDeletethanks For sharing this top notch article.i exploit this newsletter to function my venture in college.it's far beneficial For me amazing paintings. Sony Vegas Pro 20
ReplyDeleteMalwarebytes Anti Malware Premium Serial Key that is my first duration i go to right here and i discovered consequently many enthralling stuff for your blog specifically it is aeration, thank you.
ReplyDeleteThanks for sharing the nice information. Sapphire Solutions is the best
ReplyDeleteNodeJS development company providing quality NodeJS development services to build secure, scalable, user-friendly and faster web applications.
nice information thanks for sharing....!
ReplyDeletespring boot certification course training
I'd like you to know that this software is great and easy to use.
ReplyDeleteAdobe Illustrator CC
Really its very useful information that you have shared and looks great post.. If you looking for company which provide best service for ExpressJS than connect with CodersNews. they listed Top 5 Industry-reviewed ExpressJS Web Development Agencies
ReplyDeleteInformative blog, Thanks for sharing. Looking for node js development services? Get the best node js web development services based on usa and India.
ReplyDeleteExcellent blog. Thank you for providing such useful information. Infowind tech is a Development company that provides the best Node.js development services
ReplyDeletein the IT industry. Hire Nodejs Developers if you are interested.
Informative blog, Thanks for sharing.
ReplyDeletehire nodejs developers to build high performing web aplications with ReactJS. Also Looking for best node js web development company? Choose 3Brain Technologies one of the best node ja company based on USA, India
Great article on setting up a web server with Express! For those interested in expanding their web development skills further, consider checking out the "Best Laravel Training Course in Surat" to enhance your expertise in another popular framework.
ReplyDeletehttps://decodex.io/laravel-training-course-in-surat
Great article! Node.js has certainly revolutionized the way we approach web development, especially with its non-blocking, event-driven architecture. For businesses looking to scale efficiently, Node.js development services are the go-to solution, offering high performance and real-time capabilities. Working with a professional Node.js development company ensures that your projects are built with expertise, making use of the latest technologies and best practices. Whether you're building a dynamic web application or handling complex backend systems, Node.js offers the versatility and speed necessary to meet modern demands. Highly recommend looking into it!
ReplyDelete