2

New to nodejs world. Trying to create a REST Service with express and I'm stuck trying to debug this issue... I want to call a GET resource that has a route param in router.use('/applications/:appId/messages', messageRoutes);

index.js

import Promise from 'bluebird';
import mongoose from 'mongoose';
import config from './config/env';
import app from './config/express';

// plugin bluebird promise in mongoose
mongoose.Promise = Promise;

// connect to mongo db
mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } });
mongoose.connection.on('error', () => {
  throw new Error(`unable to connect to database: ${config.db}`);
});

const debug = require('debug')('express-mongoose-es6-rest-api:index');

// listen on port config.port
app.listen(config.port, () => {
  debug(`server started on port ${config.port} (${config.env})`);
});

export default app;

/config/express.js

import express from 'express';
import bodyParser from 'body-parser';
import routes from '../server/routes';

const app = express();

// parse body params and attache them to req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

[...]

// mount all routes on /api path
app.use('/api', routes);


[...]

export default app;

/server/routes/index.js

import express from 'express';
import userRoutes from './user';
import messageRoutes from './message';
import applicationRoutes from './application';
import authRoutes from './auth';

const router = express.Router(); // eslint-disable-line new-cap

// mount message routes at /messages
router.use('/applications/:appId/messages', messageRoutes);

export default router;

/server/routes/message.js

import express from 'express';
import validate from 'express-validation';
import paramValidation from '../../config/param-validation';
import messageCtrl from '../controllers/message';

const router = express.Router(); // eslint-disable-line new-cap

router.route('/')
   /** GET /api/applications/:appId/messages - Get list of messages */
  .get(messageCtrl.getByAppId);

export default router;

/server/controllers/message.js

import mg from 'mongoose'
import Message from '../models/message';
import moment from 'moment';

function getByAppId(req, res, next) {
  //return res.json(req.message);
  console.log(req);
}

export default {getByAppId};

The result of console.log(req) is a printout of the request object but the params array is blank.

 baseUrl: '/api/applications/57fcf8129eb1d52f1cb10332/messages',
 originalUrl: '/api/applications/57fcf8129eb1d52f1cb10332/messages/',
 _parsedUrl:
  Url {
    protocol: null,
    slashes: null,
    [...]
 params: {},
 query: {},

Can anyone offer any guidance? Much appreciated. Not sure why the params are blank. Could it be the :appId variable declaration in express router.use() statement?

melmack
  • 95
  • 7
  • Where and how do you mount the api routes? `/api/applications/:appId/... Also it would be better if you added new code block with filename instead of using ==== . – Molda Oct 12 '16 at 04:13
  • Does this answer your question? [Rest with Express.js nested router](https://stackoverflow.com/questions/25260818/rest-with-express-js-nested-router) – Ivan Rubinson Oct 15 '20 at 22:19

0 Answers0