简体   繁体   English

在server.js文件外部进行快速路由

[英]Express routing outside of server.js file

I've followed numerous Express/Angular guides so far for routing, and in the scenarios each work, and I've gotten the hang of it but I'm having a slight issue when I try to incorporate the route into -another- JS file. 到目前为止,我已经遵循了大量的Express / Angular指南进行路由,并且在每种情况下都可以使用,并且已经掌握了很多技巧,但是当我尝试将路由合并到另一个JS时遇到了一个小问题文件。

For instance I put this in server.js, it'll work. 例如,我将其放在server.js中,它将起作用。 If I put it in 'char.js' it won't work. 如果我将其放在“ char.js”中,它将无法正常工作。

I'm using a package that allows these functions to pull Blizzard game data. 我正在使用允许这些功能提取暴雪游戏数据的软件包。 The end-points work directly, but trying to get the functions to work inline. 端点直接起作用,但是试图使这些函数内联。

server.js -- The file that npm opens with 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))

This function will work - If I go to /Users, I will get the json response. 此功能将起作用-如果我转到/ Users,我将得到json响应。

If I move it to 如果我将其移动到

Chars.js 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;

It no longer will work. 它不再起作用。 I have the -same- snipper, but with diff name for the chars variable in my Users controller that is working 100% outside of that function. 我有-same- snipper,但是在我的Users控制器中chars变量的名称与diff名称相同,该变量在该函数之外的工作率为100%。

I'm unsure exactly what it is I'm missing. 我不确定我到底缺少什么。

Entire Server.js 整个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}!`);
});

I ran this by the package owner and he stated the code I have in Chars.js should work so I feel like it's something regarding my placement of files, or variables. 我是由包所有者运行的,他说我在Chars.js中拥有的代码应该可以工作,所以我觉得这与我文件或变量的放置有关。 I did console.log on chars.js and got a response from server.js so I do know it's loading it. 我在chars.js上做了console.log并得到了server.js的响应,所以我知道它正在加载它。

If an additional script is required, let me know. 如果需要其他脚本,请告诉我。 That could also be my issue because I have only been modifying these two scripts. 那也可能是我的问题,因为我只修改了这两个脚本。

Updated code to reflect changes 更新了代码以反映更改

Don't create another express instance in Chars.js . 不要在Chars.js创建另一个Express实例。

1 Remove 1移除

const app = express()

2 Move 2移动

app.use('/', chars);

from Chars.js to Server.js Chars.jsServer.js

working example: 工作示例:

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

const express = require('express');
const router = express.Router();

router.get('/users', function(req, res) {
//       return blizzard.wow.character(['profile'], { origin: 'us', realm: 'Myrealm', name: 'Mychar' })
//                 .then(response => res.json(response.data));

    setTimeout(function() {
      res.json({data: 'some data'})
    }, 2000);
});

module.exports = router;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM