This is a reference cheat sheet for Express, a flexible and streamlined web framework for Node.js
$ mkdir myapp # create directory $ cd myapp # enter the directory $ npm init -y # Initialize a configuration
$ npm install express
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res. send('Hello World!') }) app. listen(port, () => { console.log("Server is running") })
router. use(function (req, res, next) { //.. some logic here .. like any other middleware next() })
//depends on where the router "use()" router. get('/events', (req, res, next) => { //.. })
app.delete('/', function (req, res) { res.send('DELETE request to homepage') })
app.disable('trust proxy') app.get('trust proxy') // => false
var engines = require('consolidate') app.engine('haml', engines.haml) app.engine('html', engines.hogan)
var express = require('express') var app = express() app.listen(3000)
function logOriginalUrl (req, res, next) { console.log('ReqURL:', req.originalUrl) next() } function logMethod (req, res, next) { console.log('Request Type:', req.method) next() } const log = [logOriginalUrl, logMethod] app.get('/user/:id', log, (req, res, next)=>{ res.send('User Info') } )
app.set('view engine', 'pug') app.get('/', (req, res) => { res. render('index', { title: 'Hey', message: 'Hello there!' }) })