Hello World | Express JS
I hope you have a better understanding of Express JS. If you are not familiar with why and what about Express JS and basics, I highly recommend you to read the first article on this article series and follow onwards to deeply understand everything about ExpressJS and it’s usage.
Prerequisites:
Curiosity, interest and a tiny bit knowledge of Node.js, also you need to have node installed on your system. For detailed explanations and installation guide please refer to the following article.
Tools:
Any code editor will work. I prefer Visual Studio Code as it gives me a complete environment and can be customized as required.
What we’re building:
A simple express js starter “Hello World” program, where if user hit browser on specific port, browser screen will show ‘Hello World’ to user.
Let’s Start:
Step 1: Generate package.json file
For every nodejs application it is first step to generate package.json file which is having all the dependency and script to automate tasks define.
Commands used: cd <project_folder_path>&& npm init
Step 2: Install express node package
Command used: npm install express --save
Note: After successfully installation you will be able to see below updates in project directory
Two new entries: node_modules folder and package-lock.json file.
package.json file update will have dependencies entry like below
“dependencies”: {
“express”: “4.17.1”
}
Step 3: Write and execute server.js file by following below steps
a) Create server.js file at root level.
b) Write below code in server.js file
Now to execute server.js file there are two way
- In terminal execute node server.js and hit http://localhost:3000 in browser
- Define start script in package.json file having command ‘node server.js’ and run npm start command in terminal and hit http://localhost:3000 in browser
Congratulation !! You are now able to write and execute express js code and we are good for next series ‘CRUD Demo in ExpressJS’
I hope you have enjoyed this article. To make this article more interesting please reach me @LinkedIn
Stay tuned for Next article: ExpressJS Implementation and documentation with Swagger