Coggle requires JavaScript to display documents.
const cb0 = (req, res, next) => {}; const db1 = (req, res, next) => {}; app.get('/example/d', [cb0, cb1], (req, res, next) => {}, (req, res) => {})
const router = express.Router(); router.use(middleware); router.get('/', (req, res) => {}) const posts = require('./posts'); app.use('/posts', posts)
export const middleware = (options) => (req, res, next) => {}; app.use(middleware({...options})
app.use((req, res, next) => { ... next(); }); app.use('/user/:id', (req, res, next) => { ... next(); }); app.use('/user/:id', (req, res, next) => { next(); }, (req, res, next) => { next(); }; );
app.get('/user/:id', (req, res, next) => { if (xxx) next('route') else next(); }, (req, res, next) => { res.send('xxx'); } );
app.get('/user/:id', [middleware1, middleware2], (req, res, next) => {}))
app.use((err, req, res, next) => { console.error(err.stack); res.stats(500).send('sth wrong'); });
html head title = title body h1= message
app.get('/', (req, res) => { res.render('index', { title: 'Hey', message: 'Hello there!' }); });
app.get('/', (req, res) => { throw new Error('BROKEN'); });
app.get('/', (req, res, next) => { fs.readFile('/file-not-exits', (err, data) => { if (err) { next(err); // pass errors to Express. } else { res.send(data); } }); });
app.get('/user/:id', async (req, res, next) => { const user = await getUserById(req,params.id); res.send(user); });
app.get('/', [ function (req, res, next) { fs.writeFile('/inaccessible-paht', 'data', next); }, function (req, res) { res.send('OK'); }, ]);
app.get('.', (req, res, next) => { setTimeout(() => { try { throw new Error('BROKEN'); } catch (err) { next(err); } },100); });
app.get('.', (req, res, next) => { Promise.resolve().then(() => { throw new Error('BROKEN'); }).catch(next); });
app.get('/', [ function (req, res, next) { fs.readFile('/maybe-valid', 'utf-8', (err, data) => { res.locals.data = data; next(err); }); }, function (req, res) { res.locals.data = res.locals.data.split(',')[1]; res.send(res.locals.data); }, ]);
const bodyParser = require('body-parser'); const methodOverride = require('method-override'); app.use(bodyParser.urlencoded({ extended: true }); app.use(bodyParser.json()); app.use(methodOverride()); app.use((err, req, res, next) => {});
app.use(logErrors); app.use(clientErrorHandler); app.use(errorHandler);
app.set('trust proxy', (ip) => { if (ip === '127.0.0..1' || ip = '123.123,123.123') return true; else return false; })
const mysql = require('mysql'); const connection = myusql.createConnection({ host: 'localhost', user: 'dbuser', password: 'secret', database: 'my_db', }); connection.connect(); connection.query('SELECT XXX',(err, rows, fields) => {}); connection.end();
app.response.sendStatus = function (statusCode, type, message) { return this.contentType(type).status(statusCode).send(message); }; res.sendStatus(404, 'application/json', '{"error":"resource not found"}');
Object.defineProperty(app.request, 'ip', { configurable: true, enumerable: true, get() { return this.get('Client-IP'); }, })
app.get('/search', (req, res) => { setImmediate(() => { const jsonStr = req.query.params; try { const jsonObj = JSON.parse(jsonStr); res.send('Success'); } catch (e) { res.status(400).send('Invalid JSOn string'); } }); });
// in route handler app.get('/', async (req, res, next) => { const data = await userData(); // if promise fails, it will automatically call `next(err)` to handle the error res.send(data); }); app.use((err, req, res, next) => { res.status(error.status ?? 500).send({ error: err.message }); }); // in middleware app.use(async (req, res, next) => { req.locals.user = await getUser(req); next(); });
[Unit] Description=<Awesome Express App> [Service] Type=simple ExecStart=/usr/local/bin/node </projects/myapp/index.js> WorkingDirectory=</projects/myapp> User=nobody Group=nogroup Environment=NODE_ENV=production LimitNOFILE=infinity LIMITCORE=infinity StandardInput=null StandardOutput=syslog StandardError=syslog Restart=always [Install] WantedBy=multi-user.target
app.use((req, res, next) => { res.status(404).send('Sorry cannot find that!'); })
app.use((err, req, res, next) => { res.status(500).send('Something broke!'); })
const session = require('express-session'); app.set('trust proxy', 1); app.use( session({ secret: 's3cur3', name: 'sessionId', }) );
const session = require('cookie-session'); const express = require('express'); const app = express(); const expiryDate = new Date(Date.now() + 60 * 60 * 1000); app.use( session({ name: 'session', keys: ['key1', 'key2'], cookie: { secure: true, httpOnly: true, domain: 'example.com', path: 'foo/bar', expires: expiryDate, }, }) );
const server = app.listen(port); process.on('SIGTERM', () => { debug('SIGTERM signal received: closing HTTP server'); server.close(() => { debug('HTTP server closed'); }); });
const express = require('express'); const app = express();