- Basics
- Environment & Practices
- Middleware
- Render Views with Templates
- EJS
- Routes
- Error Handling
- RESTful / Hypermedia
- HTTP2 / SPDY
- Web-Events / Feeds
- Web-Sockets
npm install express --save
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World'));
const server = app.listen(3000, () => {
const address = server.address() || {};
console.log(
`Example app listening at http://${address.host}:${address.port}`,
);
});
- Use the "right" logging
- Use Process-Manager (forever / PM2)
- Use
NODE_ENV=production
||NODE_ENV=development
- Use Compression & Caching
- Handle exception with (try/catch) / Promise-Catch and prefer Fail-Fail (avoid handling 'uncaughtException')
- Use ENVIRONMENT VARIABLES
- Use SIGNALS (SIGTERM / SIGHUB) for graceful shutdowns
- Static Files
- Compression
- Body-Parser
- Cookie-Parser
- CORS
- Multi-Part
- Request/Response Time
- Session
- Authentication (Passport)
app.use(express.static('./public'));
const compression = require('compression');
app.use(compression());
npm install morgan --save
const morgan = require('morgan');
app.use(morgan());
npm install ejs --save
- EJS
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/index.html', (req, res) => res.render('index', { title: 'Title' }));
<%
'Scriptlet' tag, for control-flow, no output<%=
Outputs the value into the template (escaped)<%-
Outputs the unescaped value into the template<%#
Comment tag, no execution, no output-%>
Trim-mode ('newline slurp') tag, trims following newline
- use
<%- include('common/footer') %>
app
.route('/books')
.get((req, res) => res.send('Get a random book'))
.post((req, res) => res.send('Add a book'))
.put((req, res) => res.send('Update the book'));
const express = require('express');
const router = express.Router();
// middleware that is specific to this router
router.use((req, res, next) => {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', (req, res) => res.send('Home page'));
// define the about route
router.get('/about', => (req, res) => res.send('About page'));
module.exports = router;
const books = require('./books');
// ...
app.use('/books', books);
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello GET'));
app.post('/', (req, res) => res.send('Hello POST'));
app.delete('/del_user', (req, res) => res.send('Hello DELETE'));
app.get('/ab*cd', (req, res) => res.send('Page Pattern Match'));
const server = app.listen(3000, () => {
const address = server.address() || {};
console.log(
`Example app listening at http://${address.host}:${address.port}`,
);
});
app.use('*', (req, res) => {
res.render('404', err);
});
app.use((err, req, res, next) => {
// handle error
console.error(err);
next();
});
app.get('/', (req, res, next) => {
doAsync()
.then(data => res.send(data))
.catch(next);
});
Next generation of HTTP Protocol
- HTTPS by default
- Duplex communication with pushing resource support
- SPDY2/3, HTTP/1.1/1.0 fallback support
const spdy = require('spdy');
const express = require('express');
const fs = require('fs');
const app = express();
spdy
.createServer(
{
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt'),
},
app,
)
.listen(8443, error => {
if (error) console.error(error);
if (error) return process.exit(1);
console.log(`Listen on ${SERVICE_PORT}`);
});
Works like a continuous data feed
app.get('/subscribe/:feedName', (req, res) => {
res.set('Transfer-Encoding', 'chunked');
stream.on(req.params.feedName || 'progress', data => {
res.write(JSON.stringify(data) + '\n');
});
});
npm install ws --save
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({ port: 3000 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
window.onload = function() {
var socket = new WebSocket('ws://localhost:3000');
socket.onopen = function(event) {
console.log(event);
// Send an initial message
socket.send("I am the client and I'm listening!");
// Listen for messages
socket.onmessage = function(event) {
console.log('Client received a message', event);
};
// Listen for socket closes
socket.onclose = function(event) {
console.log('Client notified socket has closed', event);
};
// To close the socket....
//socket.close()
};
};