创建一个目录,起名为website,这就是我们以后的工程目录。
一、创建package.json文件
进入该目录,在命令行输入 npm init ,为工程创建一个package.json文件,命令要求输入一些参数,如工程名、版本号等,按回车接受默认设置。
$ npm 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>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (website) project
version: (1.0.0)
description:
git repository:
keywords:
license: (ISC)
About to write to /Users/zhaoshanshan/work/website/package.json:
{
"name": "project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "zss",
"license": "ISC",
"devDependencies": {
"express": "^4.16.2"
},
"dependencies": {},
"description": ""
}
Is this ok? (yes)
二、安装express并创建一个项目
安装express,在命令行执行:
$ npm install express --save
由于express3+把创建app的功能分离出来为express-generator,因此需要再安装express-generator:
$ npm install express-generator --save
(mac需要root用户安装,输入sudo -s,进入root用户即可)
接下来输入(-e:指定使用 ejs 作为模板引擎):
express -e project
然后进入project目录,输入npm install 安装项目依赖包。
这样就创建了一个名为project的项目,目录结构如下:
其中:
- app.js:启动文件
- package.json:存放项目信息及配置依赖
- node_modules:存放项目依赖库
- public:存放 image、js、css等文件
- routes:存放路由文件
- views:存放视图文件
- bin:存放可执行文件
命令行输入:npm start,打开浏览器输入地址:http://localhost:3000/ ,可以看到 :
下一节,继续介绍gulp及webpack。