1. Introduce
Express.js, commonly referred to as Express, is a minimal and flexible web application framework for Node.js. It provides a robust set of features for building web and mobile applications, including web servers, APIs, and web applications.
2. Preparing
You have to install node js in your pc and have code editor ( We recommend use VS code)
3. Let start
– Initialize app
mkdir myapp
cd myapp
npm init
– installing express js
npm install express
– Create Basic router. Express js router allow you can create router to send request to express app (express server). Create file index.js and add code bellow:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Save file and run node index.js in root app folder, You can see result:
– Access url http://localhost:3000/ on Browser, You can see “Hello World!” Result is showing.
– The simple app is ok for now but as you can see, if you want to edit or update code , You need stop server and run it again. it will take more time, So you can use nodemon to to make hot reload ( You do not need restart server after editing code)
npm install --save-dev nodemon
– Update code start server in package.json
...
"scripts": {
"start": "nodemon index.js", //<-- add it
"test": "echo \"Error: no test specified\" && exit 1"
},
...
Now, start server and try edit something.
npm start
Reload app in Browser, You can see the response is updated.
Conclusion: Here is simple express js app, I hope you can create your express js app and use it for your project, if you have any question, You can comment in this post, We can discuss further.