在node.js中导出这些REST API函数
Export these REST API functions in node.js
我正在尝试从模块中导出一些REST API函数。我正在使用node.js restify。
我有一个名为rest.js
的文件,其中包含API。
module.exports = {
api_get: api_get,
api_post: api_post,
};
var api_get= function (app) {
function respond(req, res, next) {
res.redirect('http://127.0.0.1/login.html', next);
return next();
}; //function respond(req, res, next) {
// Routes
app.get('/login', respond);
}
var api_post= function (app) {
function post_handler(req, res, next) {
};
app.post('/login_post', post_handler);
}
API是以这种方式调用的;
var rest = require('./rest');
var server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
rest.api_get(server);
rest.api_post(server);
遇到的错误是TypeError: rest.api_get is not a function
您的错误是在定义函数变量之前导出它们。正确的方法是在底部进行导出。一直这样做也是一种很好的做法。正确的代码应该是这样的;
var api_get= function (app) {
function respond(req, res, next) {
res.redirect('http://127.0.0.1/login.html', next);
return next();
}; //function respond(req, res, next) {
// Routes
app.get('/login', respond);
}
var api_post= function (app) {
function post_handler(req, res, next) {
};
app.post('/login_post', post_handler);
}
module.exports = {
api_get: api_get,
api_post: api_post,
};
相关文章:
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 在同一ip但不同端口上使用Rest API
- 从ANGULAL连接到REST API
- 如何在使用loopback REST API时更改gulp-angular项目的端口号
- Cross domain - REST API
- 更改REST API服务器的最佳实践
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- REST API node.js
- 数据未显示 - 余烬 js - Rest API
- 登录后无法使用 ACS REST API 发送推送通知
- 在 Rest API 中发布调用
- REST API 服务器是否应该使用 PUT 接收 JSON 数据或普通表单数据
- 如何使用已经存在的 REST API 后端在 Angular 应用程序中实现登录系统
- 获取文件并从rest api下载
- 使用Angular Resource查询具有字符串参数的REST API
- Visual Studio Team Services Rest API未授权的客户端错误
- 无法通过rest api读取json变量
- Wordpress REST API身份验证,无需登录页面
- 通过REST API/GET请求订阅Mailchimp列表