node.js - Meteor picker server side router use express middleware -
i trying use express functions res.send('string')
or res.json(json)
in meteor rest api using picker server side router. in documentation, says :
you can use existing connect , express middlewares without issues.
how can use express funtions res.send , res.json ? when try use them, tells me not function.
i have following main.js file server :
import { meteor } 'meteor/meteor'; import { picker} '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 following route action (routes/helloworld.js) :
function helloworld(params, req, res, next) { res.send('id:' + params.id); } module.exports = helloworld;
i following error :
typeerror: res.send not function
it yells same error when try 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 found can mimic res.json 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;
as picker/meteor not using express, not have res.send() , res.json().
however, can explore restivus, high level api wrapper handle json automatically.
http://meteorpedia.com/read/rest_api#restivus
following sample code above link:
if(meteor.isserver) { meteor.startup(function () { // global configuration api = new restivus({ version: 'v1', usedefaultauth: true, prettyjson: true }); // generates: get/post on /api/v1/users, , get/put/delete on /api/v1/users/:id // meteor.users collection (works on mongo collection) api.addcollection(meteor.users); // that's it! many more options 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 add article"} }; } } }); }); }
Comments
Post a Comment