在expressjs中创build模型

我有一个快速的应用程序,从外部API获取其数据

api.com/companies (GET, POST) api.com/companies/id (GET, PUT) 

我想创build一个模型,使代码更容易维护,你可以看到我在这里重复了很多代码。

 router.get('/companies', function(req, res, next) { http.get({ host: 'http://api.com', path: '/companies' }, function(response) { var body = ''; response.on('data', function(d) { body += d; }); }); res.render('companies', {data: body}); }); router.get('/companies/:id', function(req, res, next) { http.get({ host: 'http://api.com', path: '/companies/' + req.params.id }, function(response) { var body = ''; response.on('data', function(d) { body += d; }); }); res.render('company', {data: body}); }); 

我怎样才能做到这一点?

首先: http.get是asynchronous的。 经验法则:当你看到一个callback,你正在处理一个asynchronousfunction。 如果使用res.render终止请求,则http.get()及其callback将完成。 这意味着res.render总是需要在callback中发生。

我在这个例子中使用了ES6语法。

 // request (https://github.com/request/request) is a module worthwhile installing. const request = require('request'); // Note the ? after id - this is a conditional parameter router.get('/companies/:id?', (req, res, next) => { // Init some variables let url = ''; let template = '' // Distinguish between the two types of requests we want to handle if(req.params.id) { url = 'http://api.com/companies/' + req.params.id; template = 'company'; } else { url = 'http://api.com/companies'; template = 'companies'; } request.get(url, (err, response, body) => { // Terminate the request and pass the error on // it will be handled by express error hander then if(err) return next(err); // Maybe also check for response.statusCode === 200 // Finally terminate the request res.render(template, {data: body}) }); }); 

关于你的“模型”问题。 我宁愿将它们称为“服务”,因为模型是一些数据集合。 服务是逻辑的集合。

要创build一个公司服务模块做这样的事情:

 // File companyService.js const request = require('request'); // This is just one of many ways to encapsulate logic in JavaScript (eg classes) // Pass in a config that contains your service base URIs module.exports = function companyService(config) { return { getCompanies: (cb) => { request.get(config.endpoints.company.many, (err, response, body) => { return cb(err, body); }); }, getCompany: (cb) => { request.get(config.endpoints.company.one, (err, response, body) => { return cb(err, body); }); }, } }; // Use this module like const config = require('./path/to/config'); const companyService = require('./companyService')(config); // In a route companyService.getCompanies((err, body) => { if(err) return next(err); res.render(/*...*/) }); 

这种情况下不需要有多条路线! 你可以使用一个可选的参数 。 看看下面的例子:

 router.get('/companies/:id?', function(req, res, next) { var id = req.params.id; http.get({ host: 'http://api.com', path: '/companies/' + id ? id : "" }, function(response) { var body = ''; response.on('data', function(d) { body += d; }); }); res.render('companies', {data: body}); }); 

这里的代码位:

 path: '/companies/' + id ? id : "" 

正在使用内联if语句,所以它的意思是, 如果 id != null, false, or undefined ,则将id添加到companies/string, 否则只需添加任何内容。

编辑

关于js类,你可以这样做:

 // Seperate into a different file and export it class Companies { constructor (id) { this.id= id; this.body = ""; // You can add more values for this particular object // Or you can dynamically create them without declaring here // eg company.new_value = "value" } get (cb) { http.get({ host: 'http://api.com', path: '/companies/' + this.id ? this.id : "" }, function(response) { response.on('data',(d) => { this.body += d; cb (); // callback }); }); } post () { // You can add more methods ... Eg a POST method. } put (cb) { http.put({ host: 'http://api.com', path: '/companies/' + this.id ? this.id : "", ... Other object values here ... }, function(response) { response.on('data',(d) => { ... do something here with the response ... cb(); //callback }); }); } } 

你的路由器类可以像这样使用这个类:

 router.get('/companies/:id?', function(req, res, next) { var id = req.params.id; var company = new Companies(id); company.get(() => { // We now have the response from our http.get // So lets access it now! // Or run company.put(function () {...}) console.log (company.body); res.render('companies', {data: company.body}); }); }); 

为简单起见,我在这里添加了callback,但我build议使用promise: https : //developers.google.com/web/fundamentals/getting-started/primers/promises

处理重构的一般方法是确定代码中的不同之处,并将其提取为传递到包含通用代码的函数的dynamic部分。

例如,这里不同的两件事是请求发生的path

 '/companies' vs '/companies/:id' 

并传递给http.get的相关path

 '/companies' vs '/companies/' + req.params.id 

你可以提取这些并传递给一个函数,为你分配一个处理程序。

这是一个通用的方法:

 // props contains the route and // a function that extracts the path from the request function setupGet(router, props) { router.get('/' + props.route, function(req, res, next) { http.get({ host: 'http://api.com', path: props.getPath(req) }, function(response) { var body = ''; response.on('data', function(d) { body += d; }); }); res.render('company', { data: body }); }); } 

然后用以下两个选项来调用它:

 setupGet(router, { route: 'companies', getPath: function(req) { return 'companies'; } }); setupGet(router, { route: 'companies/:id', getPath: function(req) { return 'companies' + req.params.id; } }); 

这样做的好处是您可以使用路线和path的任意组合以及使用其他req属性来确定path。

另一件你需要意识到的事情是你的res.render调用会你执行body += d之前发生,因为前者是在调用http.get之后同步发生的,后者是asynchronous发生的(稍后)。

你可能想把render方法放在callback中。

 // props contains the route and // a function that extracts the path from the request function setupGet(router, props) { router.get('/' + props.route, function(req, res, next) { http.get({ host: 'http://api.com', path: props.getPath(req) }, function(response) { var body = ''; response.on('data', function(d) { body += d; // probably want to render here res.render('company', { data: body }); }); }); }); }