DNorthrup
DNorthrup

Reputation: 847

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.

For instance I put this in server.js, it'll work. If I put it in 'char.js' it won't work.

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

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.

If I move it to

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.

I'm unsure exactly what it is I'm missing.

Entire 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. I did console.log on chars.js and got a response from server.js so I do know it's loading it.

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

Upvotes: 0

Views: 1276

Answers (1)

Dmitriy
Dmitriy

Reputation: 340

Don't create another express instance in Chars.js.

1 Remove

const app = express()

2 Move

app.use('/', chars);

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

Upvotes: 1

Related Questions