如何使用Node JS Tools在Visual Studio中调试Yeoman生成器KO(带有typescript和gulp)Node JS项目

Dan*_*ulp 7 visual-studio node.js requirejs yeoman-generator ntvs

我已创建了在Visual Studio中的节点JS项目(2012专业2013社区)NTVS,并使用了约曼生成器来创建一个淘汰赛SPA的应用程序(使用发电机设置的打字稿选项).

现在我需要在调试时决定将哪个文件设置为启动文件(按F5).我想这将是./src/app/require.config.js因为否则我得到一个错误,需要是未定义的.

当我开始调试时,一切看起来都很好,并出现一个控制台窗口,其中显示消息"调试器正在侦听端口5858".但是当我启动localhost:5858时,没有服务器/网站.

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

所以我的问题是: - 我应该将什么设置为启动文件? - 如何使用NTVS在Visual Studio中调试我的应用程序?


编辑

我确定当我添加一个新的空NTVS项目时,它会创建一个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);
Run Code Online (Sandbox Code Playgroud)

将其设置为启动文件会导致此文件的工作调试.

我怎样才能通过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"] }
    }
};
Run Code Online (Sandbox Code Playgroud)

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 });
Run Code Online (Sandbox Code Playgroud)

编辑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);
Run Code Online (Sandbox Code Playgroud)

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

Amy*_*y B 1

好吧,经过更多的研究,我认为你想做的事情是错误的。

您正在尝试将 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`.
    // e.g. var jQuery = require("jquery")(window);
    // See ticket #14549 for more info.
Run Code Online (Sandbox Code Playgroud)

我相信您想要做的是将 html 从服务器返回到浏览器。该 html 应该通过脚本标签引用 jquery、bootstrap 和 knockout - 处理文档的客户端 javascript 文件。


另请注意:由于 jquery 和其他程序在浏览器中运行,因此您将无法使用 Visual Studio 来调试它们(据我所知)。您应该使用浏览器工具来检查它们的行为。

/sf/answers/1482467801/