dev-notes/Node.js/Libs/express.md
2021-01-31 11:05:37 +01:00

1.5 KiB

Express

Installation

npm install express --save

Application

const express = require("express");

const app = express();
const PORT = 5555;

// correctly serve static contents
app.use(express.static("/route/for/static/files", "path/to/static/files/folder"));

// HTTP GET
app.get("/<route>:<param>", (req, res) => {
    console.log(`${req.connection.remoteAddress} requested ${req.url}`);
});

// HTTP POST
app.post("/<route>:<param>", (req, res) => {
    console.log(`${req.connection.remoteAddress} posted to ${req.url}`);
});

// responds to all HTTP verbs
app.all("/<route>:<param>", (req, res, next) => { 
    next();  // handle successive matching request (valid also on .get() & .post())
});

let server = app.listen(PORT, () => {
    console.log(`Express server listening at http://localhost:${PORT}`);
});

server.on("error", () => {
    server.close();
});

Response

Response.send([body]);  // Sends the HTTP response.
Response.sendFile(path);  // Transfers the file at the given path.
Response.json(body);  // Sends a JSON response.
Response.redirect([status,] path);  // Redirects to the URL derived from the specified path, with specified status
Response.end();  // Ends the response process

Request

Request.params.<param>  // query params obj (GET)
Request.body.<param>  // body params (POST)