-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexpress.js
More file actions
82 lines (75 loc) · 2.47 KB
/
express.js
File metadata and controls
82 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// reference for this tutorial;
// http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/
// http://expressjs.com/en/starter/generator.html
// setting up / Installation
// 1. install node.js
// link -> https://nodejs.org/en/
// 2. install express generator, for mac users, add sudo in the front
// type the command below on the terminal, it is different from express.js. It creates a skeleton for using express.
// $(sudo)npm install -g express-generator
// start your app!
// these commands will create an app for you and install the packages
// $express appname
// $cd appname
// $(sudo)npm install
// To test if it worked
// start the server!
// $npm start
//structure
.
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
// Basic routing
// http://expressjs.com/en/guide/routing.html
// in index.js / users.js
APP.MODULE(PATH, HANDLER FUNCTION)
// get : read
app.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
// Other RESTful Services
// create
app.post('/', function (req, res) {
res.send('Got a POST request');
});
// update, replace
app.put('/', function (req, res) {
res.send('Got a PUT request');
});
// delete
app.delete('/', function (req, res) {
res.send('Got a DELETE request');
});
//using static files (usually included in app.js) to include files in public like BOOTSTRAP!
app.use(express.static('public'));
app.use('/static', express.static(__dirname + '/public'));
// jade
// resource: http://jade-lang.com/
// similar to HTML but it's a template and accepts variable passing to it
// if you are more comfortable with HTML, you can try EJS by either specify when start the new project
// or npm install ejs, but make sure to change the engine in app.js
// resource for ejs:
// 1. $ (sudo)npm install ejs
// app.set('view engine', 'ejs'); in app.js
// 2. $ express appname -ejs
//usual structure of these files:
// make a layout / heading .jade that is similar in all other pages
// index.jade, where the main content is
// footer.jade the footer
//final notes:
// The installation might be the hardest part, make sure your syntax correspond to the right version.
// ask google anything, he is super friendly and helpful.