Skip to main content

πŸ“¦ Custom Middleware

app.use(express.json()); This one basically allows a client to send us JSON, basically.Without this, you would have to manually put together the bits yourself to make JSON work, and that would suck, so this does it for you.

import express from "express";
import router from "./router";
import morgan from "morgan";
const app = express();

app.use(morgan("dev"));
app.use(express.json());

app.get("/", (req, res) => {
morgan;
res.status(200).send({
success: true,
message: "Welcome to the Node.js and Express API",
});
});

app.use("/api", router);

export default app;

So urlencoded allows a client to add things like a query string and parameters and it decodes and encodes that properly.

import express from "express";
import router from "./router";
import morgan from "morgan";
const app = express();

app.use(morgan("dev"));
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
app.use("/api", router);
export default app;

So I don't know if you've ever seen a query string before, but it's basically a string that comes after a URL that has a bunch of key-value pairs separated by commas or equal signs.

info

URL encoded will take all of this and put it in an object for you.

google.com?a=1,thing=otherthing

Creating Custom Middleware​