Node.js学习(一)

  • 新建文件夹:myblog
  • 控制台:
//初始化package.json 
E:
odepmyblog>cnpm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (myblog)
version: (1.0.0)
description: my first blog
entry point: (index.js)
test command:
git repository:
keywords: blog
author: zhaohe
license: (ISC)
About to write to E:
odepmyblogpackage.json:

{
  "name": "myblog",
  "version": "1.0.0",
  "description": "my first blog",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [
    "blog"
  ],
  "author": "zhaohe",
  "license": "ISC"
}


Is this ok? (yes) y
//安装express并加入package.json
E:
odepmyblog>cnpm i express@4.14.0 --save
√ Installed 1 packages
√ Linked 41 latest versions
√ Run 0 scripts
√ All packages installed (44 packages installed from npm registry, used 4s, speed 50.43kB/s, json 45(76.9kB), tarball 131.49kB)

E:
odepmyblog>node index.js
^C
E:
odepmyblog>
//安装supervisor并加入package.json
//在开发过程中,每次修改代码保存后,我们都需要手动重启程序,才能查看改动的效果。使用supervisor可以解决这个繁琐的问题
//supervisor 会监听当前目录下 node 和 js 后缀的文件,当这些文件发生改动时,supervisor 会自动重启程序。 E: odepmyblog
>cnpm i supervisor --save √ Installed 1 packages √ Linked 0 latest versions √ Run 0 scripts √ All packages installed (1 packages installed from npm registry, used 873ms, speed 12.91kB/s, json 1(2.96kB), tarball 8.31kB)
  • 文件夹目录

  • index.js
//简单的express例子
var express =require("express");
var app = express();
app.get('/',function(req ,res){
    
    res.send('Hello express!');
});
app.listen(3000);
  • package.json
{
  "name": "myblog",
  "version": "1.0.0",
  "description": "my first blog",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [
    "blog"
  ],
  "author": "zhaohe",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "supervisor": "^0.12.0"
  }
}
  •  运行结果

  •  supervisor的使用

上面的安装方式少一个设置,所以不管用,后来我直接全局安装了

键入命令: npm -g install supervisor

这里注意一点的就是,supervisor必须安装到全局,如果你不安装到全局,错误命令会提示你安装到全局。

如果不想安装到默认的全局,也可以自己修改全局路径到当前路径

npm config set prefix "路径"

安装完以后就可以用supervisor 来启动服务了。

E:
odepmyblog>cnpm -g i supervisor
D:NodeJs6.10supervisor -> D:NodeJs6.10
ode_modulessupervisorlibcli-wrapper.js
D:NodeJs6.10
ode-supervisor -> D:NodeJs6.10
ode_modulessupervisorlibcli-wrapper.js
D:NodeJs6.10
`-- supervisor@0.12.0


E:
odepmyblog>supervisor index.js

Running node-supervisor with
  program 'index.js'
  --watch '.'
  --extensions 'node,js'
  --exec 'node'

Starting child process with 'node index.js'
Watching directory 'E:
odepmyblog' for changes.
Press rs for restarting the process.
crashing child
Starting child process with 'node index.js'
crashing child
Starting child process with 'node index.js'

 

原文地址:https://www.cnblogs.com/6324/p/6759691.html