到目前为止,我已经遵循了许多Express / Angular指南进行路由,并且在每个工作的场景中,我已经掌握了它,但是当我尝试将路由合并到 - 时,我遇到了一个小问题 - 另一个 - JS文件。
例如我把它放在server.js中,它会起作用。如果我把它放在'char.js'中它将不起作用。
我正在使用一个允许这些功能来提取暴雪游戏数据的软件包。端点直接工作,但试图使函数内联工作。
server.js - npm打开的文件
app.get('/Users', function( req, res){
return blizzard.wow.character(['profile'], { origin: 'us', realm: 'Myserver', name: 'Mycharname' })
.then(response => res.json(response.data))
此功能将起作用 - 如果我转到/ Users,我将得到json响应。
如果我将其移至
Chars.js
const express = require('express');
const router = express.Router();
const account = require('../routes/account');
const wow = require('../routes/wow.js');
const sc2 = require('../routes/sc2');
const d3 = require('../routes/d3');
const blizzard = require('../config/blizzard.js');
/*
// Create a route for the 'Users' path
const char = router.get('Char', function( req, res){
return blizzard.wow.character(['profile'], { origin: 'us', realm: 'Myserv', name: 'Myname' })
.then(response => res.json(response.data))
});
*/
// Create a route for the 'Users' path
router.get('/', function(req, res) {
return blizzard.wow.character(['profile'], { origin: 'us', realm: 'Myrealm', name: 'Mychar' })
.then(response => res.json(response.data));
});
});
/*
// Create a route for the 'Users' path
const users = router.get('Users', function(req, res) {
return blizzard.wow.character(['profile'], { origin: 'us', realm: 'Myserv', name: 'Myname' })
.then(response => res.json(response.data));
});
*/
module.exports = router;
它不再起作用。我有-same- snipper,但我的Users控制器中的chars变量的diff名称在该函数之外100%工作。
我不确定究竟是什么我错过了。
整个Server.js
'use strict';
require('dotenv').config({ silent: true });
var express = require('express')
,http = require('http')
,path = require('path')
,app = express()
,fs = require('fs');
const router = express.Router();
const account = require('./routes/account');
const wow = require('./routes/wow');
const sc2 = require('./routes/sc2');
const d3 = require('./routes/d3');
const chars = require('./routes/chars.js');
const blizzard = require('./config/blizzard.js');
const port = process.env.PORT || 5000;
app.use('/account', account);
app.use('/wow', wow);
app.use('/sc2', sc2);
app.use('/d3', d3);
// Mount the 'Users' route to `/`
app.use(express.static('public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', function (req, res) {
res.render('index');
});
app.use('/', chars);
app.listen(port, () => {
console.log(`Blizzard.js Example App listening on port ${port}!`);
});
我是由程序包所有者运行的,他说我在Chars.js中的代码应该可以工作,所以我觉得这是关于我放置文件或变量的东西。我在chars.js上做了console.log并得到了server.js的响应,所以我知道它正在加载它。
如果需要其他脚本,请与我们联系。这也可能是我的问题,因为我只修改了这两个脚本。
更新了代码以反映更改
答案 0 :(得分:1)
请勿在{{1}}中创建另一个快速实例。
1删除
Chars.js
2移动
const app = express()
从app.use('/', chars);
到Chars.js
工作示例:
Server.js
server.js
'use strict';
const port = process.env.PORT || 5000;
const express = require('express')
,http = require('http')
,path = require('path')
,app = express()
,fs = require('fs');
const chars = require('./routes/chars.js');
app.use(express.static('public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => res.render('index'));
app.use('/', chars);
app.listen(port, () => console.log('online:', port));
routes/chat.js