express get and post code example

Example 1: node js express url parameters

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

Example 2: express route parameters

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

Example 3: express getting options instead of post

// 'OPTIONS' is a standard request send by browsers BEFORE your request
// Your server needs to handle these requests appropriately
// Please see source and upvote the answer I got this from to share credit

var express = require('express')
  , cors = require('cors')
  , app = express();
const corsOptions = {
  origin: true,
  credentials: true
}
app.options('*', cors(corsOptions)); // preflight OPTIONS; put before other routes
app.listen(80, function(){
  console.log('CORS-enabled web server listening on port 80');
});