express-serve-static-core#NextFunction TypeScript Examples

The following examples show how to use express-serve-static-core#NextFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: http-server.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
function middleware(publicURL: string, logger: winston.Logger) {
  function logAccessIfVerbose(req) {
    const protocol = req.connection.encrypted ? 'https' : 'http';
    const fullUrl = `${protocol}://${req.headers.host}${req.url}`;

    logger.debug(`Request: ${fullUrl}`);
  }

  return function (req: Request, res: Response, next: NextFunction) {
    logAccessIfVerbose(req);

    function send404() {
      if (next) {
        return next();
      }
      setHeaders(res);
      res.writeHead(404);
      res.end();
    }

    function sendIndex() {
      req.url = `/${path.basename('index.html')}`;
      serve(req, res, send404);
    }

    const { pathname } = url.parse(req.url);
    if (pathname.startsWith('/api')) {
      next();
    } else if (!pathname.startsWith(publicURL) || path.extname(pathname) === '') {
      // If the URL doesn't start with the public path, or the URL doesn't
      // have a file extension, send the main HTML bundle.
      return sendIndex();
    } else {
      // Otherwise, serve the file from the dist folder
      req.url = pathname.slice(publicURL.length);
      return serve(req, res, sendIndex);
    }
  };
}
Example #2
Source File: index.ts    From node-express-typescript with MIT License 6 votes vote down vote up
app.use(function(req:Request, res: Response, next:NextFunction) {

    if(!fs.existsSync(frontEndPath+req.path) && allRoutes.filter(r => req.path.startsWith(r)).length < 1){
        console.log('redirect:',req.path)
        res.sendFile(frontEndPath + 'index.html');
    } else {
        next()
    }

});
Example #3
Source File: auth.ts    From node-express-typescript with MIT License 6 votes vote down vote up
protected = (req: Request, res: Response, next: NextFunction) => {
        const authHeader = req.header('Authorization');
        if (authHeader) {
            const token = authHeader.split(' ')[1];
            const jwt: AuthObject = this.decode(token);
            if (jwt) {
                console.log(jwt)
                req.user = jwt;
                next();
            }
        }
        if(typeof req.user === 'undefined'){
            res.status(401).json({error: 'unauthorized'}).end();
        }
    }