Middleware examples
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
/* @flow */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const express = require("express");
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const bodyParser = require("body-parser");
|
||||||
|
app.use(bodyParser.urlencoded({extended:false}));
|
||||||
|
|
||||||
|
app.use("/", (req, res) => {
|
||||||
|
console.log(req.query, req.body);
|
||||||
|
res.send("Server alive, with Express!");
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.error("Error....", err.message);
|
||||||
|
res.status(500).send("INTERNAL SERVER ERROR");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(8080, () =>
|
||||||
|
console.log(
|
||||||
|
"Mini server (with Express) ready at http://localhost:8080/!"
|
||||||
|
)
|
||||||
|
);
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/* @flow */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const express = require("express");
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
console.log("Logger... ", new Date(), req.method, req.path);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
if (req.method === "DELETE") {
|
||||||
|
next(new Error("DELETEs are not accepted!"));
|
||||||
|
} else {
|
||||||
|
res.send("Server alive, with Express!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.error("Error....", err.message);
|
||||||
|
res.status(500).send("INTERNAL SERVER ERROR");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(8080, () =>
|
||||||
|
console.log(
|
||||||
|
"Mini server (with Express) ready at http://localhost:8080/!"
|
||||||
|
)
|
||||||
|
);
|
||||||
@@ -20,6 +20,11 @@ app.get("/license", (req, res) =>
|
|||||||
res.sendFile(`${flagsPath}/license.txt`)
|
res.sendFile(`${flagsPath}/license.txt`)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.error("Error....", err.message);
|
||||||
|
res.status(500).send("INTERNAL SERVER ERROR");
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(8080, () =>
|
app.listen(8080, () =>
|
||||||
console.log(
|
console.log(
|
||||||
"Mini Express static server ready at http://localhost:8080/!"
|
"Mini Express static server ready at http://localhost:8080/!"
|
||||||
|
|||||||
Reference in New Issue
Block a user