diff --git a/chapter04/src/get_parameters.js b/chapter04/src/get_parameters.js new file mode 100644 index 0000000..1723425 --- /dev/null +++ b/chapter04/src/get_parameters.js @@ -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/!" + ) +); diff --git a/chapter04/src/middleware.js b/chapter04/src/middleware.js new file mode 100644 index 0000000..862e7d6 --- /dev/null +++ b/chapter04/src/middleware.js @@ -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/!" + ) +); diff --git a/chapter04/src/serve_statics_alt.js b/chapter04/src/serve_statics_alt.js index 6da3d8c..b890622 100644 --- a/chapter04/src/serve_statics_alt.js +++ b/chapter04/src/serve_statics_alt.js @@ -20,6 +20,11 @@ app.get("/license", (req, res) => 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, () => console.log( "Mini Express static server ready at http://localhost:8080/!"