我正在使用Express在node.js中编写一个Web应用程序.我已经定义了如下路线:
app.get("/firstService/:query", function(req,res){
//trivial example
var html = "<html><body></body></html>";
res.end(html)
});
Run Code Online (Sandbox Code Playgroud)
如何从快递中重用该路线?
app.get("/secondService/:query", function(req,res){
var data = app.call("/firstService/"+query);
//do something with the data
res.end(data);
});
Run Code Online (Sandbox Code Playgroud)
我在API文档中找不到任何内容,宁愿不使用像"请求"这样的其他库,因为这看起来很笨拙.我想尽可能保持我的应用程序模块化.思考?
谢谢
我有多条路线.如何通过在组路由的GET方法中调用数据来获取用户路由(GET方法)中的数据?这样做的最佳方式是什么?
我的app.js看起来像这样:
var express = require('express');
var routes = require('./routes/index');
var users = require('./routes/users');
var groups = require('./routes/groups');
var app = express();
app.use('/', routes);
app.use('/users', users);
app.use('/groups', groups);
module.exports = app;
app.listen(3000);Run Code Online (Sandbox Code Playgroud)
然后我有另一个文件routes/users.js:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('GET ON THE users!');
});
module.exports = router;Run Code Online (Sandbox Code Playgroud)
另一条路线路线/ groups.js:
var express = require('express');
var router = express.Router();
var otherRouter = require('./users')
/* GET groups listing. */
router.get('/', function(req, …Run Code Online (Sandbox Code Playgroud)我在 Express 中设置了一个简单的 API 路由(主要通过我的 Angular 前端使用):
app.post('/api/sendEmail', emailApi.sendEmail);
Run Code Online (Sandbox Code Playgroud)
我已经制作了一个位于后端的模块,并且还需要调用此服务。我认为最简单的方法是执行 POST 请求:
request({
url: '/api/sendEmail',
method: 'POST',
json: {
template: template.toLowerCase(),
obj: mailObj
}
}, function(error, response, body){
console.log('error', error);
console.log('response', response);
console.log('body', body);
});
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
Error: Invalid URI "/api/sendEmail"
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?