-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
112 lines (96 loc) · 4.23 KB
/
server.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
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
110
111
var http = require("http");
var url = require('url');
var express = require("express");
var app = express();
path = require("path")
//app.use(express.logger());
app.configure(function () {
app.use(express.bodyParser());
});
// And mysql module you've just installed.
fs = require("fs");
app.get('/', function (request, response) {
fs.readFile('index.html', function (err, html) {
if (err) {
throw err;
}
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
});
console.log("HERE");
});
app.get('/:file', function (request, response) {
var request2 = url.parse(request.url, true);
var action = request2.pathname;
console.log(action.toLowerCase());
if (action.toLowerCase().indexOf(".jp") != -1) {
var img = fs.readFileSync('Content' + action);
response.writeHead(200, {'Content-Type': 'image/jpg' });
response.end(img, 'binary');
console.log(action);
} else if(action.indexOf(".js") != -1){
fs.readFile('Script' + action, function (err, html) {
if (err) {
throw err;
}
response.writeHeader(200, {"Content-Type": "text/javascript"});
response.write(html);
response.end();
});
console.log(action);
} else if(action.indexOf(".json") != -1){
fs.readFile('Script' + action, function (err, html) {
if (err) {
throw err;
}
response.writeHeader(200, {"Content-Type": "text/json"});
response.write(html);
response.end();
});
}
});
app.get('/admin/:id', function (request, response) {
fs.readFile('admin.html', function (err, html) {
if (err) {
throw err;
}
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
});
});
app.post('/upload', function(req, res) {
fs.readFile(req.files.image.path, function (err, data) {
var imageName = req.files.image.name
/// If there's an error
if(!imageName){
console.log("There was an error")
res.redirect("/");
res.end();
} else {
var newPath = __dirname + "/Content/" + imageName;
/// write file to uploads/fullsize folder
fs.writeFile(newPath, data, function (err) {
/// let's see it
res.redirect("/admin/123");
var imgsString;
fs.readFile("Script/Images.json", 'utf8', function (err, data) {
imgsString = data.substring(0, data.length - 1) + ",{\"image\": \"" + imageName +"\"}]";
fs.writeFile("Script/Images.json", imgsString, function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to ");
}
});
});
});
}
});
});
//app.listen(8080)
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});