express

npm install express
const express = require('express');
const app = express();
const server;

app.get('/', (req, resp) => {
    resp.sendFile('index.html');
});
server = app.listen(8000);
// index.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next){
    res.render('index', {'title': 'hello world'});
});

router.post('/', function(req, res, next){
    const city = req.body.city;
});

module.exports = router;

express

express()

Возвращает оъект сервера Server()

const app = express();

static

static()
express.static('static');

Server

class Server()

Объект сервера

Server.get(route, callback)

Задает обработчик для маршрута/урла

app.get('/', (req, res) => {
    res.sendFile('index.html');
});

// app.get('/:page?', (req, res) => {
app.get('/:page', (req, res) => {
    var page = req.params.page;
    res.redirect('/');
});
let context = {
    content: "Hello World"
};

app.get('/', (req, res) => {
    res.render('index', context);
});
Server.route()
app.route('new')
    .get((req, resp) => {})
    .post((req, resp) => {} );
Server.listen(port[, host[, callback]])

Запускает сервер по указанным параметрам

app.listen(8000);
app.listen(8000, () => console.log("Server started"));
Server.set(key, value)

Задает значения для параметров

// задаем шаблонизатор для рендеринга
app.set("view engine", "ejs");
app.set("view engine", "jade");
Server.use()

Мидлваре

app.use(function(req, res, next){
    next();
});
import apiRouter from './apiRouter';


app.use(logger(dev));
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(static_path));
app.use('/static', express.static(static_path));
app.use('/api', apiRouter);

Router

class Router()
const router = express.Router();
Router.get(url, callback)
router.get('/', (req, res) => {
    res.send("Hello World");
});

router.get('/books/:bookId', (req, res) =>{
    // req.params.bookId
});