在 expressjs 中创建模型
create models in expressjs
我有一个从外部获取数据的 Express 应用 API
api.com/companies (GET, POST)
api.com/companies/id (GET, PUT)
我想创建一个模型以使代码更易于维护,如您所见,我在这里重复了很多代码。
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});
});
我该怎么做?
进行重构的一般方法是确定代码中的不同之处,并将其提取为动态部分,然后传递到包含通用代码的函数中。
例如,这里不同的两件事是请求发生的路径
'/companies' vs '/companies/:id'
以及传递给 http.get
的相关路径
'/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;
}
});
这里的好处是您可以使用路由和路径的任意组合以及使用其他 req
属性来确定路径。
您需要意识到的另一件事是,您的 res.render
调用将在 之前 发生 body += d
,因为前者在调用之后同步发生到 http.get
,后者异步发生(稍后)。
您可能想将 render
方法放在回调本身中。
// 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
});
});
});
});
}
在这种情况下不需要有多条路线!您可以使用 ? 的可选参数。看看下面的例子:
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 语句,所以它的意思是,if id != null, false, or undefined
,将 id 添加到 companies/
字符串和 else什么都不加。
编辑
关于 js classes,你可以这样做:
// 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
// e.g 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 ... E.g 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
});
});
}
}
您的路由器 class 然后可以像这样使用此 class:
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});
});
});
为了简单起见,我在此处添加了回调,但我建议使用 promises:
https://developers.google.com/web/fundamentals/getting-started/primers/promises
首先:
http.get 是异步的。经验法则:当您看到回调时,您正在处理一个异步函数。您无法判断,当您使用 res.render 终止请求时,http.get() 及其回调是否会完成。
这意味着 res.render 总是需要在回调中发生。
我在此示例中使用 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})
});
});
关于您的 'model' 问题。
我宁愿称它们为 'services',因为模型是一些数据的集合。服务是逻辑的集合。
要创建公司服务模块,请执行以下操作:
// File companyService.js
const request = require('request');
// This is just one of many ways to encapsulate logic in JavaScript (e.g. 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(/*...*/)
});
我有一个从外部获取数据的 Express 应用 API
api.com/companies (GET, POST)
api.com/companies/id (GET, PUT)
我想创建一个模型以使代码更易于维护,如您所见,我在这里重复了很多代码。
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});
});
我该怎么做?
进行重构的一般方法是确定代码中的不同之处,并将其提取为动态部分,然后传递到包含通用代码的函数中。
例如,这里不同的两件事是请求发生的路径
'/companies' vs '/companies/:id'
以及传递给 http.get
'/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;
}
});
这里的好处是您可以使用路由和路径的任意组合以及使用其他 req
属性来确定路径。
您需要意识到的另一件事是,您的 res.render
调用将在 之前 发生 body += d
,因为前者在调用之后同步发生到 http.get
,后者异步发生(稍后)。
您可能想将 render
方法放在回调本身中。
// 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
});
});
});
});
}
在这种情况下不需要有多条路线!您可以使用 ? 的可选参数。看看下面的例子:
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 语句,所以它的意思是,if id != null, false, or undefined
,将 id 添加到 companies/
字符串和 else什么都不加。
编辑
关于 js classes,你可以这样做:
// 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
// e.g 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 ... E.g 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
});
});
}
}
您的路由器 class 然后可以像这样使用此 class:
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});
});
});
为了简单起见,我在此处添加了回调,但我建议使用 promises: https://developers.google.com/web/fundamentals/getting-started/primers/promises
首先: http.get 是异步的。经验法则:当您看到回调时,您正在处理一个异步函数。您无法判断,当您使用 res.render 终止请求时,http.get() 及其回调是否会完成。 这意味着 res.render 总是需要在回调中发生。
我在此示例中使用 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})
});
});
关于您的 'model' 问题。 我宁愿称它们为 'services',因为模型是一些数据的集合。服务是逻辑的集合。
要创建公司服务模块,请执行以下操作:
// File companyService.js
const request = require('request');
// This is just one of many ways to encapsulate logic in JavaScript (e.g. 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(/*...*/)
});