-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (41 loc) · 1.56 KB
/
Copy pathapp.js
File metadata and controls
50 lines (41 loc) · 1.56 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
// include expressjs
const express = require('express');
// use express function to app variable. express() is a top-level function exported by the express module
const app = express();
const path = require('path');
const myRoutes = require('./routes/myRoutes.js');
const PORT = process.env.PORT || 3000;
// express static folder for assets (css, images, js, libs and uploads)
app.use(express.static(__dirname + '/public'));
// setting ejs template engine for html file
// all file inside views folder with html extension can be use
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// create a get routes for '/' with arrow function
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
// another route without arrow function localhost:3000/test
app.get('/test', function(req, res){
res.send("My Test route!");
});
// home page route by get method
app.get('/home', function (req, res) {
res.send("<h1>Homepage</h1>");
});
// login page route by post method
app.post('/login', function (req, res) {
res.send("<h1>Login Page</h1>");
});
// login page route by post method
app.all('/gallery', function (req, res) {
res.send("<h1>My Gallery</h1>");
});
// user page route by post method
app.get('/user/:userId', function (req, res) {
res.send("<h1>User Profile Page by User Id: " + req.params.userId + "</h1>");
});
// use myRoutes to get all routes from there
app.use('/myRoutes', myRoutes);
// app.listen(3000);
app.listen(PORT, () => console.log('My App listening on port 3000! Express is awesome!!!'));