Practical Node.js chapter 1

启动node.js脚本

$ NODE_ENV=production API_KEY=442CC1FE-4333-46CE-80EE-6705A1896832 node server.js

Node.js 控制台 REPL

在终端敲node进入repl

1+1
a = 1;

基础知识

Node.js是建立在Google Chrome V8引擎和ECMASCRIPT之上的。

命名

静态变量或者私有函数以_开头

保留的关键词

  1. process
  2. global
  3. module.exports

__dirname和process.cwd

绝对路径
如果像这样启动
$ node ./code/program.js.

两者的路径是不一样的

核心模块

  1. htpp
  2. util
  3. querystring
  4. url
  5. fs
  6. path
  7. crypto
  8. string_decoder

调试node.js程序

  1. Node.js Debugger 并不好用
  2. Node Inspector Chrome devtools的一个端口
  3. Webstrom IDE调试 非常好用
  4. console.log

使用Node.js Debugger

node debug hello.js

next 快捷键n 跳到下一个语句
cont 快捷键c 跳到下一个断点
step s 进入function
out o 跳出function
watch

打开浏览器 http://localhost:1337

或者执行 curl http://localhost:1337

使用Node Inspector

npm install -g node-inspector

然后,启动node-inspector
node-inspector

在新的终端打开
node —debug-brk hello-debug.js or node —debug hello-debug.js

打开chrome浏览器 其它浏览器不可以
http://localhost:8080/debug?port=5858

检测文件改动

  1. forever
  2. nodemon
  3. supervisor
  4. up
原文地址:https://www.cnblogs.com/bhaltair/p/7192110.html