-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (99 loc) · 3.77 KB
/
server.js
File metadata and controls
109 lines (99 loc) · 3.77 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// const express = require('express');
// const ecstatic = require('ecstatic');
// const http = require('http');
// const app = express();
// console.log(`${__dirname}/login/dist`)
// app.use('/login', ecstatic({ root: `${__dirname}/login/dist`, showdir: false }))
// http.createServer(app).listen(8010)
// console.log('Listening on :8080')
var http = require('http')
var fs = require('fs')
var path = require('path')
const PORT = 8010
http.createServer(function (request, response) {
let filePath = request.url
console.log(`filePath: ${filePath}`)
let basePath = 'login'
if (filePath === '/login') {
basePath = 'login'
filePath = '/'
} else if (filePath === '/ppl/estudiantes') {
basePath = 'ppl/estudiantes'
} else if (filePath === '/assessment/profesores' || filePath.startsWith('/assessment/profesores')) {
basePath = 'assessment/profesores'
} else if (filePath === '/att/profesores' || filePath.startsWith('/att/profesores')) {
basePath = 'att/profesores'
} else if (filePath === '/att/estudiantes' || filePath.startsWith('/att/estudiantes')) {
basePath = 'att/estudiantes'
}
const extname = path.extname(filePath)
if (filePath == '/') {
filePath = `./${basePath}/dist/index.html`
} else if (filePath === '/assessment/profesores') {
filePath = `./${basePath}/dist/index.html`
} else if (filePath.startsWith('/assessment/profesores')) {
filePath = filePath.split('/').slice(3).join('/')
} else if (filePath === '/att/estudiantes' || filePath === '/att/profesores') {
filePath = `./${basePath}/dist/index.html`
} else if (filePath.startsWith('/att/estudiantes') || filePath.startsWith('/att/profesores')) {
filePath = filePath.split('/').slice(3).join('/')
} else {
filePath = filePath.split('/').slice(2).join('/')
}
// ./att/estudiantes/dist/estudiantes/estudiantes/js/vendor.2cffdd77d6eef556dce9.js
console.log(`basePath: ${basePath}`)
let contentType = 'text/html'
switch (extname) {
case '.js':
filePath = `./${basePath}/dist/` + filePath
contentType = 'text/javascript'
break
case '.map':
filePath = `./${basePath}/dist/` + filePath
contentType = 'text/javascript'
break
case '.css':
filePath = `./${basePath}/dist/` + filePath
contentType = 'text/css'
break;
case '.json':
contentType = 'application/json'
break;
case '.png':
filePath = `./${basePath}/dist/` + filePath
contentType = 'image/png'
break;
case '.jpg':
filePath = `./${basePath}/dist/` + filePath
contentType = 'image/jpg'
break
case '.wav':
contentType = 'audio/wav'
break
}
let p = path.join(__dirname, filePath)
if (p[p.length - 1] === '/') {
p = p.substring(0, p.length - 1)
}
console.log(`filePath: ${filePath}\n`)
fs.readFile(p, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType })
response.end(content, 'utf-8')
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n')
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType })
response.end(content, 'utf-8')
}
});
}).listen(PORT)
console.log(`Server running at http://127.0.0.1:${PORT}/`)