如何在不更改/ bin / www的情况下在Visual Studio 2015中调试Node.JS?

时间:2015-12-12 08:56:50

标签: node.js visual-studio express visual-studio-2015 express-4

为了提高工作效率,我开始使用Visual Studio 2015而不是Sublime Text for Node JS和Express.js 4项目。使用Express 4和npm标准的新结构,项目的起点位于带有www文件的bin文件夹中,但Visual Studio的调试器始终了解起始点是主文件夹中的app.js文件。因此,当我按下Run / Debug按钮时,我的浏览器中没有任何内容出现在设置端口中。我不想更改www和app.js文件中的任何内容,因为整个环境都有一些功能代码(声明websocket,webRTC等)。

如何在不进行任何更改的情况下调试Visual Studio 2015?

这是/ 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);
}

1 个答案:

答案 0 :(得分:1)

从我看到的,你还没有开始使用给定的VS NodeJs Tools模板,但我认为你已经安装了它。

  

Visual Studio始终了解起点是app.js   文件

据我所知,这不完全正确(首先检查package.json)。

您检查了package.json吗?

{
  "name": "MySuperDuperNodeJsApp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "prestart": "gulp"
  }, ....

如果你的package.json中的脚本对象指向正确的开始动作,它应该可以工作。