简体   繁体   English

如何调试yeoman应用程序?

[英]How to debug a yeoman application?

Coming from the background of developing web apps using server-side languages/platforms such as java, python/django and php, I am starting to learn Node.js and yeoman. 来自使用服务器端语言/平台(如java,python / django和php)开发Web应用程序的背景,我开始学习Node.js和yeoman。 I consider being able to debug server-side code essential in order to improve code quality. 我认为能够调试服务器端代码是必不可少的,以提高代码质量。 Node supports debugging via node-inspector and --debug option. Node支持通过node-inspector和--debug选项进行调试。 But if an application is created out of yeoman using a generator, say AngularJS, and launched using grunt, is there an easy way to enable debugging? 但是,如果使用生成器(例如AngularJS)使用生成器创建应用程序,并使用grunt启动,是否有一种简单的方法来启用调试?

Perhaps a higher level question I should first ask is: If yeoman promotes (as featured in tutorial) AngularJS, which is a client-side MVC framework, then majority of code will run from browser rather than from Node. 也许我应该首先提出的一个更高级别的问题是:如果yeoman推广(如教程中所述)AngularJS,它是一个客户端MVC框架,那么大多数代码将从浏览器而不是从Node运行。 Is that the reason why server-side debugging is not important any more to yeoman and therefore not documented? 这就是为什么服务器端调试对于自耕农来说不再重要的原因,因此没有记录?

Simple like that: 很简单:

npm install -g node-inspector
node-debug yo GENERATOR_NAME

It's gonna open a Chrome DevTool with an initial breakpoint. 它会打开一个带有初始断点的Chrome DevTool。

I am developing a yemoan angular-fullstack application. 我正在开发一个yemoan angular-fullstack应用程序。 I debug the server side node js application code as follows; 我调试服务器端节点js应用程序代码如下;

  1. Install node-inspector . 安装节点检查器
  2. Modify the application 'Gruntfile.js' to set debug as 'true' for express server 修改应用程序'Gruntfile.js',将Express的设置为'true'

express: { dev: { options: { script: 'server/app.js', debug: true } } }

  1. Start the application by running grunt serve. 通过运行grunt serve启动应用程序。
  2. In a separate terminal run node-inspector . 在单独的终端运行node-inspector
  3. Open chrome and navigate to http://127.0.0.1:8080/debug?port=5858 . 打开chrome并导航到http://127.0.0.1:8080/debug?port=5858 (If you see the grunt console, you would see the node debugger is on port 5858). (如果你看到grunt控制台,你会看到节点调试器在端口5858上)。
  4. Now you should be able to put breakpoints and debug in your server application code. 现在,您应该能够在服务器应用程序代码中添加断点和调试。 You could place debugger statement in your code to force node to break at that point and wait for your inspection. 您可以在代码中放置debugger语句,以强制节点在该点断开并等待检查。

Note: node-inspector --no-preload loads the node inspector quicker. 注意: node-inspector --no-preload可以更快地加载节点检查器。

  1. install node-inspector : 安装node-inspector

    $ npm install -g node-inspector $ npm install -g node-inspector

  2. start Node Inspector server: 启动Node Inspector服务器:

    $ node-inspector $ node-inspector

  3. run node.js with Yeoman-cli in debug mode: 在调试模式下使用Yeoman-cli运行node.js:

    $ node --debug path\\to\\global\\npm\\node_modules\\yo\\cli.js MyGenerator $ node --debug path \\ to \\ global \\ npm \\ node_modules \\ yo \\ cli.js MyGenerator

    where 'MyGenerator' is Yeoman generator name which you want to debug. 其中'MyGenerator'是您要调试的Yeoman生成器名称。
    On Windows "path\\to\\global\\npm" is something like "C:\\Users{UserName}\\AppData\\Roaming\\npm". 在Windows上,“path \\ to \\ global \\ npm”类似于“C:\\ Users {UserName} \\ AppData \\ Roaming \\ npm”。

  4. open Chrome or Opera and go to http://localhost:8080/debug?port=5858 打开Chrome或Opera,然后转到http://localhost:8080/debug?port=5858

If you use AngularJS and Chrome you can use Batarang plugin . 如果您使用AngularJSChrome ,则可以使用Batarang插件 In Yeoman project, you can use Grunt to check your application: 在Yeoman项目中,您可以使用Grunt检查您的应用程序:

Maybe the answer is not generic to all yeoman generated apps but I figured it out with angular-fullstack generator, which has express as backend server. 也许答案不是所有yeoman生成的应用程序的通用,但我想出了angular-fullstack生成器,它表示为后端服务器。 Debug is enabled by setting debug to true for express config in file Gruntfile.js: 通过在文件Gruntfile.js中为express配置将debug设置为true来启用调试:

grunt.initConfig({
     ...
 express : {
    options : {
        port : process.env.PORT || 9000
    },
    dev : {
        options : {
            script : 'server.js',
            debug : true //enable debugging
        }
    },
    ...

Then node-inspector can be launched to debug on port 5858 by default. 然后,默认情况下可以启动node-inspector以在端口5858上进行调试。

first enable the debug mode in Gruntfile.js 首先在Gruntfile.js中启用调试模式

grunt.initConfig({
...
express : {
options : {
    port : process.env.PORT || 9000
},
dev : {
    options : {
        script : 'server.js',
        debug : true //enable
    }
},
...

save and run the project with this command grunt serve:debug , now you can use the debugger; 使用此命令保存并运行项目grunt serve:debug ,现在可以使用debugger; var in any part of your project. var在项目的任何部分。

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

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