What is Express.js and how do I create a basic app?
Asked on Sep 19, 2024
Answer
Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. Here's how you can create a basic Express.js app:
<!-- BEGIN COPY / PASTE -->
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 at http://localhost:${port}`);
});
<!-- END COPY / PASTE -->
Additional Comment:
✅ Answered with JavaScript best practices.- Install Express.js using npm with the command: "npm install express".
- The code creates a simple server that listens on port 3000.
- When accessing the root URL ("/"), it responds with "Hello World!".
- Run the app using the command: "node filename.js", replacing "filename.js" with your file's name.
Recommended Links:
← Back to All Questions