expressjsHow do I use the Express.js API to create a web application?
To create a web application using Express.js API, you need to install the Express.js framework and its dependencies. After installation, you can create an Express.js application using the following code:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000, () => console.log('Server running on port 3000'))
This code will create a web application that will listen on port 3000 and will respond with "Hello World!" when it receives a GET request on the root URL.
The code contains the following parts:
- The
require('express')
statement imports the Express.js library and assigns it to theexpress
variable. - The
express()
statement creates an Express.js application and assigns it to theapp
variable. - The
app.get()
statement sets up a route handler for the root URL. - The
res.send()
statement sends a response with the given string. - The
app.listen()
statement starts the web server and listens on the given port.
Output example
Server running on port 3000
Helpful links
More of Expressjs
- How do I find Express.js tutorials on YouTube?
- How do I download a zip file using Express.js?
- How do I manage user roles in Express.js?
- How can I use express-zip js to zip and download files?
- How do I download a zip file using Express.js?
- How do I use Yarn to add Express.js to my project?
- How can I use Express.js to make an XHR request?
- How can I disable the X-Powered-By header in Express.js?
- How do I use Express.js to parse YAML files?
- How do I use Express.js to handle asynchronous requests?
See more codes...