9951 explained code solutions for 126 technologies


expressjsHow can I use the features of Express.js?


Express.js is a web application framework for Node.js. It provides a robust set of features for web and mobile applications. Here are some of the features of Express.js and how to use them:

  1. Routing: Express.js provides a routing API that allows you to map URLs to specific functions within your application. For example, the following code will map the URL /hello to a function that prints "Hello World!":
var express = require('express');
var app = express();

app.get('/hello', function(req, res) {
    res.send('Hello World!');
});
  1. Middleware: Express.js provides a middleware API that allows you to add custom logic to your application. For example, the following code will add a custom logger to your application that logs all requests to the console:
var express = require('express');
var app = express();

app.use(function(req, res, next) {
    console.log(req.method, req.url);
    next();
});
  1. Templates: Express.js provides a templating engine that allows you to render HTML pages with dynamic data. For example, the following code will render a page with the message "Hello World!":
var express = require('express');
var app = express();

app.get('/', function(req, res) {
    res.render('index', { message: 'Hello World!' });
});
  1. Static Files: Express.js provides a static file server that allows you to serve static files such as images, CSS, and JavaScript. For example, the following code will serve the file /public/style.css when the URL /style.css is requested:
var express = require('express');
var app = express();

app.use(express.static('public'));

These are just a few of the features of Express.js. For more information, please see the Express.js documentation.

Edit this code on GitHub