我怎样才能debugging一个Yeoman发电机KO(与打字稿和吞咽)节点JS项目在Visual Studio与节点JS工具为Visual Studio

我已经在Visual Studio(2012专业和2013社区)中用NTVS创build了一个Node JS项目,并使用Yeoman生成器来创buildKnockout SPA应用程序 (使用生成器设置中的打字稿选项)。

现在我需要决定在debugging时打开哪个文件作为启动文件(打F5)。 我想这将是./src/app/require.config.js,否则我得到一个错误,需要是未定义的。

当我开始debugging一切看起来不错,一个控制台窗口出现消息“debugging器正在监听端口5858”。 但是当我启动localhost:5858时,没有服务器/网站。

我可以在另一个端口上的服务器启动应用程序,但是没有中断点,甚至没有启动文件。

所以我的问题是: – 我应该作为启动文件设置? – 如何使用NTVS在Visual Studio中debugging我的应用程序?


编辑

我已经确定,当我添加一个新的空NTVS项目时,它创build一个server.js文件:

var http = require('http'); var port = process.env.port || 1337; http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(port); 

将其设置为启动文件会导致对该文件进行debugging。

我怎么能仍然通过require.config.js加载require并启动我的应用程序startup.ts?

require.config.js

 // require.js looks for the following global when initializing var require = { baseUrl: ".", paths: { "bootstrap": "bower_modules/components-bootstrap/js/bootstrap.min", "crossroads": "bower_modules/crossroads/dist/crossroads.min", "hasher": "bower_modules/hasher/dist/js/hasher.min", "jquery": "bower_modules/jquery/dist/jquery", "knockout": "bower_modules/knockout/dist/knockout", "knockout-projections": "bower_modules/knockout-projections/dist/knockout-projections", "signals": "bower_modules/js-signals/dist/signals.min", "text": "bower_modules/requirejs-text/text" }, shim: { "bootstrap": { deps: ["jquery"] } } }; 

startup.ts

 import $ = require("jquery"); import ko = require("knockout"); import bootstrap = require("bootstrap"); import router = require("./router"); // Components can be packaged as AMD modules, such as the following: ko.components.register('nav-bar', { require: 'components/nav-bar/nav-bar' }); ko.components.register('home-page', { require: 'components/home-page/home' }); // ... or for template-only components, you can just point to a .html file directly: ko.components.register('about-page', { template: { require: 'text!components/about-page/about.html' } }); ko.components.register('grid-page', { require: 'components/grid-page/grid-page' }); // [Scaffolded component registrations will be inserted here. To retain this feature, don't remove this comment.] // Start the application ko.applyBindings({ route: router.currentRoute }); 

编辑2

经过进一步调查,我可以启动我的应用程序与一个server.js文件作为启动文件包含

 var http = require('http'); var app = require('./src/app/startup.js'); var port = process.env.port || 1337; http.createServer(app).listen(port); 

但是这会导致'define is not defined'错误。

我认为你正在混淆服务器开发和浏览器开发。 一个nodejs项目是作为你的机器上的一个进程运行的,可以作为一个networking服务器作为许多其他的事情。 您所指的淘汰SPA应用程序是为了在浏览器环境中运行。 只有Yeoman生成器和一些te工具作为knockout模板构build过程的一部分(例如gulp)需要nodejs,但这只是在构build时需要,而不是在运行自己的代码时。

当你想在浏览器中debuggingjavascript时,你需要find你的主要index.html文件,并在浏览器中打开这个文件 – 一些IDE可以在内置的服务器上打开一个页面并直接debugging它们(例如webstorm) ,其他人可能会要求您在浏览器中打开该页面,并附上一个JavaScriptdebugging器。 在Visual Studio中,您可能应该在Internet Explorer中打开它,并在VS中的js代码中放置一个断点,我不确定是否也可以直接从VS打开一个页面,如果您需要调整IE设置比我知道的更好)

好,经过多一点研究,我认为你所要做的事是错误的。

您正试图将jquery加载到节点环境中。 Node是一个无浏览器的执行环境。 没有窗户。 没有文件。 来自jquery文件顶部的评论:

  // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get jQuery. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // eg var jQuery = require("jquery")(window); // See ticket #14549 for more info. 

我相信你想要做的是从服务器返回一个HTML到浏览器。 这个html应该引用jquery,bootstrap和knockout – 通过脚本标签处理文档的客户端JavaScript文件。


另外请注意:由于jQuery和其他人正在浏览器中运行,您将无法使用Visual Studio来debugging它们(据我所知)。 你应该使用浏览器工具来检查他们的行为。

https://stackoverflow.com/a/21178111/8155