To improve productivity, I am starting to use Visual Studio 2015 instead of Sublime Text for Node JS and Express.js 4 projects. With the new structure of Express 4 and npm standard, the starting point of the project is in bin folder with www file, but the debugger of Visual Studio always understands that the starting point is the app.js file in main folder. So when I press the Run/Debug button, nothing appears in my browser in set port. I don't want to change anything in www and app.js file because there is some feature code for whole environment (declaring websocket, webRTC, etc).
How do I let Visual Studio 2015 debug without any changes there?
This is the code in /bin/www:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('VisualMarket:server');
var http = require('http');
var easyrtc = require("easyrtc"); // EasyRTC external module
var socketHandler = require("../routes/socketHandler");
var keyManager = socketHandler.keyManager;
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
var io = app.io;
io.attach(server);
// Start EasyRTC server
var rtc = easyrtc.listen(app, io);
easyrtc.setOption("roomAutoCreateEnable", true);
// easyrtc.setOption("roomDefaultEnable", false);
// http://www.cirstei.ro/webrtc-authentication-how-i-did-it/
// http://stackoverflow.com/questions/30299728/how-to-replace-auto-generated-easyrtc-id-with-your-applications-username-in-easy
var onAuthenticate = function(socket, easyrtcid, appName, username, credential, easyrtcAuthMessage, next){
var token = credential && credential.token;
// Not success
if(!token){
socket.emit("invalid token",{
error: 1,
messages: "invalid token"
})
return;
}
// Success
next(null);
};
easyrtc.events.on("authenticate", onAuthenticate);
easyrtc.events.on('roomCreate', function(appObj, creatorConnectionObj, roomName, roomOptions, callback) {
console.log('------>>>>> ROOM CREATE ' + roomName);
return easyrtc.events.defaultListeners.roomCreate(appObj, creatorConnectionObj, roomName, roomOptions, callback);
});
easyrtc.events.on("roomJoin", function(connectionObj, roomName, roomParameter, callback) {
console.log("from roomJoin :["+connectionObj.getEasyrtcid()+"] Credential retrieved!", connectionObj.getFieldValueSync("credential"));
// if roomName is exist, do nothing
// if not && if client is shop owner, create a room with shopName, else do nothing
console.log('easyrtcid: ', connectionObj.getEasyrtcid(), connectionObj.isAuthenticated());
return easyrtc.events.defaultListeners.roomJoin(connectionObj, roomName, roomParameter, callback);
});
easyrtc.events.on('roomLeave', function(connectionObj, roomName, next) {
console.log('------>>>>> ROOM LEAVE for ', connectionObj.getEasyrtcid());
return easyrtc.events.defaultListeners.roomLeave(connectionObj, roomName, next);
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
From what I see, you have not started with given VS NodeJs Tools templates yet I assume you've installed it.
Visual Studio always understands that the starting point is the app.js file
As far as I know that is not totally correct (it first checks package.json).
Did you check package.json?
{
"name": "MySuperDuperNodeJsApp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"prestart": "gulp"
}, ....
If you have scripts object in your package.json pointing right starting action, it should work.