Table of Contents

Express

console.log("hello wordld");

Express Routing & Middleware

import express from 'express';
import {JsBin} from './db.js';


const dbReadKey = process.env.JSBIN_ACCESS_KEY
                || "$2b$10$ZT0hO3hBks9DS34Q/wljm.iRbwDGm91hSrvhP9WqHGE6dU13bZt.u";
const db = new JsBin(dbReadKey);

const app = express();

let todos = [
    { id: 1, isComplete: true, name : 'do sometgin'},
    { id: 2, isComplete: true, name : 'do sometgin'},
    { id: 3, isComplete: false, name : 'do sometgin'},
    { id: 4, isComplete: false, name : 'do sometgin'},
    { id: 5, isComplete: false, name : 'do sometgin'},
]

const constants = {
        DB_READ_KEY: process.env.JSBIN_ACCESS_KEY
};

middleware,

middleware are functions that called before the routing, has access to req, res and next. next() must be called or else the route handlers won't get called and client will timeout app.use() need to be called to enable the middleware [x] next, calls the next middleware

const logger = (req, res, next) => {
        req.id = new Date();
    console.log('logger | requestId', req.id);
        next();
}

app.use(logger);

Route Handlers

we can have app(routeString, handler1(req, res)) app(routeString, middleware(req, res, next), handler2(req, res)) app(routeString, middleware(req, res, next), , , handler2(req, res)) app(routeString, [middleware(req, res, next), handler2(req, res)])

app.get('/', rootInputHandler, rootController, rootOutputHandler);
app.get('/array', [rootInputHandler, rootController, rootOutputHandler]);
app.get('/partialarray', rootInputHandler, [rootController, rootOutputHandler]);

Query Parameters

req.query gives us access to query parameters

app.get('/ml/random/', rootInputHandler, (req, res) => {
        const min = parseInt(req.query.min);
        const max = parseInt(req.query.min);
        console.log(max)
        res.send({ num: (min + max) / 2})
});

Express.Router

this gives a mini-app, modular way to organize resources.

  • can also have middleware which deals with only its routes
const pdfRouter = express.Router();

pdfRouter.use((req, res, next) => {
        console.log("pdfRouter | middleware");
        next();
});

Route parameters

[x] path, ':name can be accesed via req.params will have obj {name: name in url} to download it using curl curl -o {yourfilename} url

pdfRouter.get('/:name', rootInputHandler, (req, res) => {
        res.download(`pdf/${req.params.name}`)
});

Response Object

  • json
  • send
  • sendStatus
  • download
  • redirect
app.get('/v1/:name', rootInputHandler, (req, res) => {
        res.redirect(`../../pdf/${req.params.name}`)
});

app.use('/pdf', pdfRouter);

app.get('/gf/', rootInputHandler, (req, res) => {
        res.sendStatus(404)
});

storing them in a jsonbin

app.get('/waifu/', rootInputHandler, async (req, res) => {
        const binName = '63353f49e13e6063dcb9c0a8';
        const waifus = await db.readBin(binName);
        res.send(waifus);
});

MIDDLEWARE

route level middleware

const isValid = () => parseInt(Math.random() * 10) % 2 == 0;

function rootInputHandler(req, res, next) {
        console.log("routeInputHandler middleware", req.id);

        if (isValid()) {
                next()
        }
        else {
                console.log('routeInputHandler | invalid input');
                res.send('invalid input');
        }
}

function rootController (req, res, next) {
        console.log("route controller ", req.id);
    req.data = {'message': 'hello world'};
        next();
}

built in middleware

  • - express static serve as a FTP server
const staticOptions = {
                "index": true,
                "extensions": ["html", "pdf"]
};
app.use("/web/", express.static("public", staticOptions));
app.use("/doc/", express.static("pdf", staticOptions));

// express.json middleware to parse json payload

app.use(express.json({limit:"1mb"}));

/*

  • Response Object
  • =============
  • */
const port = 8080;

app.listen(port, () => {
        console.log('server listening on port', port);
});