nodejs express post example

Example 1: nodejs express return image

var filepath = '~/path/to/file.png'

app.get('/path/for/site', function (req, res) {
    res.sendFile(filepath);
})

Example 2: node js express url parameters

// http://localhost:8080/api/1
app.get('/api/:version', function(req, res) {
    res.send(req.params.version);
});

Example 3: using multiparty with node js express

var express = require('express');
var multiparty = require('connect-multiparty'),
    multipartyMiddleware = multiparty({ uploadDir: './imagesPath' });

var router = express.Router();

router.post('/', multipartyMiddleware, function(req, res) {
  console.log(req.body, req.files);
  var file = req.files.file;
  console.log(file.name);
  console.log(file.type);
  res.status(200).send('OK');
});

module.exports = router;

Example 4: express route parameters

app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})