V
V
vasIvas2015-09-03 01:12:50
Node.js
vasIvas, 2015-09-03 01:12:50

How to set up routing in express.js 4.+.+?

I've been googling all day and can't figure out how to make routing in express.js 4.+.+
If someone has a successful experience, then share how to make routes '/', get: '/users/:name' post: '/users/:name'
All I could google was to put the router in the facade and first of all connect it to app.use(RouteFacade.DEFAULT_ROUTER) and then pass it to build routing, but it doesn't work...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Dragalev, 2015-09-03
@vasIvas

In one of my projects I have everything set up like this:

var express = require('express');
var routes = require('./routes/index');
var api = require('./routes/api');

var app = express();

app.use('/', routes);
app.use('/api', api);

api.js content
var express = require('express');
var router = express.Router();
router.get('/doc/all', function(req, res) {
  var query = "SELECT * FROM lectures";
  db.query(query,function(err,rows){
    if (!err) {
      console.log("User request all Docs");
      res.json(rows);
      }
    else
      console.log('Error while performing Query.');
  });
});

router.get('/doc/:docId/answers', function(req, res) {
  var query = "SELECT  answers.vopr_id AS qid, answers.id, " +
      "answers.text AS atext, answers.sequence, answers.truth  " +
      "FROM answers, questions " +
      "WHERE answers.vopr_id = questions.id " +
      "AND questions.doc_id = ?";

  var docID = [req.params.docId];
  console.log("User request answers for document with ID: " + req.params.docId);
  query = mysql.format(query,docID);
  db.query(query,function(err,rows){
    if (!err)
      res.json(rows);
    else
      console.log('Error while performing Query.');
  });
});
module.exports = router;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question