- expressjs.com – Express application generator
การติดตั้ง express generator
ติดตั้งครั้งเดียว ไม่ได้ขึ้นกับโปรเจ็กส์
npm install express-generator -g
ดู version
> express --version 4.16.1
ดู help
> express -h
Usage: express [options] [dir]
Options:
--version output the version number
-e, --ejs add ejs engine support
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-v, --view <engine> add view <engine> support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
--no-view use static html instead of view engine
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
-h, --help output usage information
-v, --view แสดง view engine ที่ใช้ได้ dust | ejs | hbs | hjs | jade | pug | twig | vash
ทดลองสร้างโปรเจ็กส์
สร้างโปรเจ็กส์ด้วย express generator โดยใช้ Pug
> express --view=pug myapp
create : myapp\
create : myapp\public\
create : myapp\public\javascripts\
create : myapp\public\images\
create : myapp\public\stylesheets\
create : myapp\public\stylesheets\style.css
create : myapp\routes\
create : myapp\routes\index.js
create : myapp\routes\users.js
create : myapp\views\
create : myapp\views\error.pug
create : myapp\views\index.pug
create : myapp\views\layout.pug
create : myapp\app.js
create : myapp\package.json
create : myapp\bin\
create : myapp\bin\www
change directory:
> cd myapp
install dependencies:
> npm install
run the app:
> SET DEBUG=myapp:* & npm start
จะได้ไฟล์ package.json
{
"name": "myapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "2.0.0-beta11"
}
}
เสร็จแล้วติดตั้ง dependencies:
> cd myapp > npm install
สั่งรันโปรเจ็กส์บน MacOS or Linux
$ DEBUG=myapp:* npm start
สั่งรันโปรเจ็กส์บน Windows
> set DEBUG=myapp:* & npm start
ลองเปิด browser เข้าที่ http://localhost:3000/
จะเห็นข้อความ
Express
Welcome to Express