首页 > 代码库 > Express的使用(一)

Express的使用(一)

  • 新建文件夹:myblog

 

  • 控制台:
//初始化package.json 
E:\nodep\myblog>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:\nodep\myblog\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"
}


Is this ok? (yes) y
//安装express并加入package.json
E:\nodep\myblog>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:\nodep\myblog>node index.js
^C
E:\nodep\myblog>
//安装supervisor并加入package.json
//在开发过程中,每次修改代码保存后,我们都需要手动重启程序,才能查看改动的效果。使用supervisor可以解决这个繁琐的问题
//supervisor 会监听当前目录下 node 和 js 后缀的文件,当这些文件发生改动时,supervisor 会自动重启程序。 E:\nodep\myblog
>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"
  }
}

 

  •  运行结果

技术分享

 

Express的使用(一)