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 use Express.js to handle asynchronous requests?
- How do I use adm-zip with Express.js?
- How can I use Express.js to generate a zip response?
- How can I use Zipkin to trace requests in Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I set up X-Frame-Options in ExpressJS?
- How can I use Express.js and Vite together for software development?
- How do I download a zip file using Express.js?
- How do I write unit tests for ExpressJS?
- How do I render a template using Express.js?
See more codes...