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 theexpressvariable. - The
express()statement creates an Express.js application and assigns it to theappvariable. - 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 parse YAML files?
- How do I use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How do I implement CSRF protection in an Express.js application?
- How do I use Express.js and Yarn together in a software development project?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I use Express.js to make an XHR request?
- How can I use Express.js and Babel together to develop a web application?
- How do I send a file using Express.js?
- How can I use express-zip js to zip and download files?
See more codes...