简体   繁体   English

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

[英]How can I debug a Yeoman generator KO (with typescript and gulp) Node JS project in Visual studio with Node JS Tools for visual studio

I have created a Node JS project in Visual Studio (2012 professional and 2013 community) with NTVS and used a Yeoman generator to create a Knockout SPA app (using the typescript option in the generator setup). 我已创建了在Visual Studio中的节点JS项目(2012专业2013社区) NTVS ,并使用了约曼生成器来创建一个淘汰赛SPA的应用程序 (使用发电机设置的打字稿选项)。

Now I need to decide which file to set as the startup file when debugging (hitting F5). 现在我需要在调试时决定将哪个文件设置为启动文件(按F5)。 I suppose this would be ./src/app/require.config.js because otherwise I get an error that require is undefined. 我想这将是./src/app/require.config.js因为否则我得到一个错误,需要是未定义的。

When I start debugging everything looks fine and a console window appears with the message "Debugger is listening to port 5858". 当我开始调试时,一切看起来都很好,并出现一个控制台窗口,其中显示消息“调试器正在侦听端口5858”。 But when I start localhost:5858, there is no server/website. 但是当我启动localhost:5858时,没有服务器/网站。

I can start the app in a server on another port but then no breakpoints are being hit, not even in the startup file. 我可以在另一个端口上的服务器中启动应用程序但是没有命中断点,甚至在启动文件中也没有。

So my questions are: - what should I set as the startup file? 所以我的问题是: - 我应该将什么设置为启动文件? - how do I debug my app in Visual Studio with NTVS? - 如何使用NTVS在Visual Studio中调试我的应用程序?


Edit 编辑

I have determined that when I add a new empty NTVS project it creates a server.js file with: 我确定当我添加一个新的空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);

Setting this as the startup file results in working debugging for this file. 将其设置为启动文件会导致此文件的工作调试。

How can I still load require through require.config.js and start my app with startup.ts? 我怎样才能通过require.config.js加载require并使用startup.ts启动我的应用程序?

require.config.js 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 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 });

Edit 2 编辑2

upon further investigation I can start my app with a server.js file as startup file containing 经过进一步调查,我可以使用server.js文件启动我的应用程序作为启动文件包含

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

but this results in the 'define is not defined' error. 但这会导致'define is not defined'错误。

I think you are mixing up server development and browser development. 我认为你正在混淆服务器开发和浏览器开发。 A nodejs project is meant to run as a process on your machine and could act as a webserver amongst many other things. nodejs项目旨在作为您计算机上的进程运行,并且可以充当许多其他事物中的Web服务器。 The knockout SPA app you are referring is meant to run in a browser environment. 您所指的淘汰SPA应用程序旨在在浏览器环境中运行。 Only the Yeoman generator and some of te tools set up as part of the build process for the knockout template (gulp for example) need nodejs, but this is only required at build time, not when running your own code. 只有Yeoman生成器和一些te工具设置为淘汰模板(例如gulp)的构建过程的一部分需要nodejs,但这只在构建时需要,而不是在运行自己的代码时。

When you want to debug javascript in a browser you need to locate your main index.html file and open that one in a browser - some IDEs have functions to open a page on a built-in server and debug them directly (webstorm for example), others might require you to open the page in a browser and attache a javascript debugger. 当您想在浏览器中调试javascript时,您需要找到主index.html文件并在浏览器中打开它 - 某些IDE具有在内置服务器上打开页面并直接调试它们的功能(例如webstorm) ,其他人可能会要求您在浏览器中打开页面并附加一个javascript调试器。 In Visual Studio you probably should open it in Internet Explorer and put a breakpoint in the js code in VS, I'm not certain if you also can directly open a page from VS and if you need to adjust IE settings (ask google, it knows better than me) 在Visual Studio中你可能应该在Internet Explorer中打开它并在VS中的js代码中放置一个断点,我不确定你是否也可以直接从VS打开一个页面,如果你需要调整IE设置(请问google,它知道比我好

Ok, after a little more research, I think the thing you are trying to do is wrong. 好的,经过一番研究,我认为你要做的事情是错的。

You are attempting to load jquery into a node environment. 您正在尝试将jquery加载到节点环境中。 Node is a browserless execution environment. Node是一个无浏览器的执行环境。 There is no window. 没有窗户。 There is no document. 没有文件。 Comment from the top of the jquery file: 来自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.

I believe what you want to do instead is return an html from your server to a browser. 我相信你要做的是从你的服务器返回一个HTML到浏览器。 That html should reference jquery, bootstrap and knockout - the client side javascript files that work on document - via script tags. 那个html应该引用jquery,bootstrap和knockout - 用于文档的客户端javascript文件 - 通过脚本标记。


Also note: since the jquery and others are running in a browser, you will be unable to use visual studio to debug them (as far as I can tell). 另请注意:由于jquery和其他浏览器在浏览器中运行,因此您将无法使用visual studio对其进行调试(据我所知)。 You should instead use browser tools to examine their behavior. 您应该使用浏览器工具来检查它们的行为。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM