-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
68 lines (56 loc) · 1.89 KB
/
main.js
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
const express = require('express');
const path = require('path');
const app = express();
const port = 8000;
const fetch = require("node-fetch");
app.listen(port);
console.log(`Server started at http://localhost:${port}`);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/app/views'));
// =================================================
// Static Assets
// =================================================
app.use('/', express.static(path.resolve(__dirname, 'public')));
// =================================================
// Dynamic (Compiled) Assets
// =================================================
// project-level `/dist` folder (generated by build)
app.use(express.static(path.resolve(__dirname, 'dist'), {
etag: false
}));
// =================================================
// Routes
// =================================================
// This route contains all of our REST endpoints
app.use('/api', require(path.resolve('app/src/js/api-routes.js')));
// Endpoints
const customersEndpoint = `http://localhost:${port}/api/customers`
// Test EJS page
app.get('/test-ejs', async (req, res) => {
try {
const api_res = await fetch(customersEndpoint);
const data = await api_res.json();
res.render('pages/index', {
title: 'Detail',
data: data
});
} catch(e) {
console.error(e);
}
});
// Customer Detail Page
app.get('/detail', function(req,res) {
res.sendFile(path.resolve(__dirname, 'app', 'views', 'detail.html'));
});
// This is the original page we worked on
app.get('/index', function (req, res) {
res.sendFile(path.resolve(__dirname, 'app', 'views', 'index.html'));
});
// Error Page (Temporary)
app.get('/error', function (req, res) {
res.sendFile(path.resolve(__dirname, 'app', 'views', '404.html'));
});
// Sign-in / Error?
app.get('/', function (req, res) {
res.sendFile(path.resolve(__dirname, 'app', 'views', 'signin.html'));
});