Reputation: 131
The client library js file is not loading. Trying to load socket.io into my project but the client is not detecting the javascript library that is supposed to be served automatically. here is my code.
var express = require('express')
, passport = require('passport')
, util = require('util')
, session = require('express-session')
, SteamStrategy = require('../../').Strategy
, Steam = require('machinepack-steam')
, path = require('path')
, app = express()
, http = require('http')
, socket = require('socket.io')
;
app.set('port', process.env.PORT || 2053);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a hot socket');
});
var steamkey = 'redacted';
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new SteamStrategy({
returnURL: 'https://get-a.team/auth/steam/return',
realm: 'https://get-a.team',
apiKey: steamkey
},
function(identifier, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
profile.identifier = identifier;
return done(null, profile);
});
}
));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(session({
secret: 'your secret',
name: 'name of session id',
resave: true,
saveUninitialized: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join('public')));
app.get('/', function(req, res){
if(req.user){
res.redirect('/account');
} else {
res.render('index', { user: req.user });
}
});
app.get('/search/:appid', ensureAuthenticated, function(req, res){
if(Number.isInteger(+req.params.appid)){
res.render('search', { user: req.user, appid: req.params.appid });
} else {
res.render('error', { user: req.user, error: "Wrong url format "+req.params.appid });
}
})
app.get('/account', ensureAuthenticated, function(req, res){
Steam.getOwnedGames({
steamid: req.user.id,
key: steamkey,
include_appinfo: 1,
include_played_free_games: 1,
appids_filter: [],
}).exec({
// An unexpected error occurred.
error: function(err) {
console.error(err);
},
// OK.
success: function(result) {
//console.log(result);
// save every game that isnt already saved
res.render('account', { user: req.user, games: result });
},
});
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.get('/auth/steam',
passport.authenticate('steam', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/');
});
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/account');
});
app.listen(3000);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
Any help will be greatly appreciated
I've tried many different ways to include the socket library online but none works, even code copied directly from the socket.io website.
I tried localhost:2053/socket.io/socket.io.js and it still won't work. Not sure what im doing wrong
Upvotes: 0
Views: 370
Reputation: 131
Figured it out. Had to reload socket.io to a different port:
...
, app2 = require('express')()
, http2 = require('http').Server(app2)
;
var io = require('socket.io')(http2);
io.on('connection', function(socket){
console.log('a user connected');
});
app2.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
http2.listen(2085, function(){
console.log('listening on *:2085');
});
the create a nginx rule to serve the lib from that port:
location /route/ {
rewrite ^/route/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:2085;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
and finally load it from the client end:
<script src="/route/socket.io/socket.io.js"></script>
if anyone knows the correct way of doing it, please let me know. This works though, feels quite like forcing a round peg in a square hole.
Upvotes: 1