Meteor server-side router uses express middleware

I'm trying to use express functions like res.send('string') or res.json(json) in my meteor shower using a picker server router. The documentation says:

You can use existing connections and express middlewares without any questions.

How to use express functions like res.send and res.json? When I try to use them, it tells me that they are not a function.

I have the following main.js file for my server:

 import { Meteor } from 'meteor/meteor'; import { Picker} from 'meteor/meteorhacks:picker'; var bodyParser = Meteor.npmRequire('body-parser'), methodOverride = Meteor.npmRequire('method-override'), logger = Meteor.npmRequire('morgan'); Picker.middleware(bodyParser.json()); Picker.middleware(bodyParser.urlencoded({extended:false})); Picker.middleware(logger('dev')); Picker.middleware(methodOverride('X-HTTP-Method')); // Microsoft Picker.middleware(methodOverride('X-HTTP-Method-Override')); // Google/GData Picker.middleware(methodOverride('X-Method-Override')); Meteor.startup(() => { console.log('meteor server started'); var postRoutes = Picker.filter(function(req, res) { return req.method == "POST"; }); postRoutes.route('/post/:id', require('./routes/helloworld')); }); 

And the following route action (routes / helloworld.js):

 function helloworld(params, req, res, next) { res.send('id:' + params.id); } module.exports = helloworld; 

I get the following error:

 TypeError: res.send is not a function 

It screams the same error when I try to use res.json ...

packages.json:

 { "body-parser": "1.15.2", "chai": "3.5.0", "chai-http": "3.0.0", "method-override": "2.3.6", "mocha": "3.0.2", "moment": "2.14.1", "moment-timezone": "0.5.5", "morgan": "1.7.0", "supertest": "2.0.0", "supertest-as-promised":"4.0.0", "express":"4.14.0" } 

UPDATE I found that I can play res.json with this code:

 function helloworld(params, req, res, next) { console.log(req.body); res.setHeader( 'Content-Type', 'application/json' ); res.end( JSON.stringify({id:params.id}) ); } module.exports = helloworld; 
+5
source share
1 answer

Since Picker / Meteor does not use an expression, it does not have res.send () and res.json ().

However, you can explore Restivus, which is a high-level api shell that automatically processes json.

http://meteorpedia.com/read/REST_API#Restivus

Below is a sample code from the link above:

 if(Meteor.isServer) { Meteor.startup(function () { // Global configuration Api = new Restivus({ version: 'v1', useDefaultAuth: true, prettyJson: true }); // Generates: GET/POST on /api/v1/users, and GET/PUT/DELETE on /api/v1/users/:id // for Meteor.users collection (works on any Mongo collection) Api.addCollection(Meteor.users); // That it! Many more options are available if needed... // Maps to: POST /api/v1/articles/:id Api.addRoute('articles/:id', {authRequired: true}, { post: { roleRequired: ['author', 'admin'], action: function () { var article = Articles.findOne(this.urlParams.id); if (article) { return {status: "success", data: article}; } return { statusCode: 400, body: {status: "fail", message: "Unable to add article"} }; } } }); }); } 
+1
source

All Articles