-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
217 lines (172 loc) · 6.37 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// static_server.js
// =========================================
// Simple node.js server to serve static file content
// Node.js HTTP static file server with ES6+
// REF : https://adrianmejia.com/blog/2016/08/24/building-a-node-js-static-file-server-files-over-http-using-es6/
const http = require('http');
const https = require('https');
const url = require('url');
const fs = require('fs');
const path = require('path');
var request = require('request');
var querystring = require('querystring');
// you can pass the parameter in the command line. e.g. node static_server.js 3000
const port = process.argv[2] || 3001;
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
// parse URL
const parsedUrl = url.parse(req.url);
// extract URL path
let pathname = `.${parsedUrl.pathname}`;
// maps file extention to MIME types
const mimeType = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.eot': 'appliaction/vnd.ms-fontobject',
'.ttf': 'aplication/font-sfnt'
};
fs.exists(pathname, function (exist) {
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
return;
}
// if is a directory, then look for index.html
if (fs.statSync(pathname).isDirectory()) {
pathname += './index.html';
}
// read file from file system
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext;
// if the file is found, set Content-type and send data
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
res.end(data);
}
});
});
if (req.method === 'POST') {
let body = [];
req.
on('error', (err) => {
console.error(err);
}).
on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
let parsedData = JSON.parse(body);
let imageNewName = cleanImageName(parsedData.coverImage);
// Save image to disk from URL
if (imageNewName) {
saveImageToDisk(parsedData.coverImage, `../src/assets/images/${imageNewName}`);
// downloadImageToUrl(parsedData.coverImage, `../src/assets/images/${'123_'+imageNewName}`, function () {});
}
generateMarkdownFile(parsedData);
/* let responseBody = {
'message': 'success'
}
res.write(JSON.stringify(responseBody));
res.end(); */
});
} else {
// res.statusCode = 404;
//res.end();
}
}).listen(parseInt(port));
// Cleanup image name
// ==============================
function cleanImageName(imagename) {
let imageName = imagename.split('/').pop();
if (imageName.includes('?')) {
imageName = imageName.split('?')[0]
}
imageName = imageName.replace(/[*!@#$%^&()\[\]_]/, '-');
return imageName;
}
// Generate Markdown file from HTML and save to disk
// ==============================
function generateMarkdownFile(parsedData) {
let filePath = '../src/' + parsedData.filePath || '../src/pages/test.html'
let markdownCode = parsedData.frontmatter + parsedData.markdownCode;
let dirName = parsedData.category.toLowerCase().trim();
fs.exists(path.join(__dirname, `../src/pages/${dirName}`), function (exists) {
if (!exists) {
fs.mkdirSync(path.join(__dirname, `../src/pages/${dirName}`));
// Generate .md file
fs.writeFile(filePath, markdownCode, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log(`Markdown file generated successfully in ${filePath}`);
});
} else {
// Generate .md file
fs.writeFile(filePath, markdownCode, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log(`Markdown file generated successfully in ${filePath}`);
});
}
});
}
// Method 1 : Node.js Function to save image from External URL.
// Note : Getting error in writing file to local disk while behind VPN
// ==============================
function saveImageToDisk(url, localPath) {
// var fullUrl = url;
var file = fs.createWriteStream(localPath);
var client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
var request = client.get(url, function (resp, body, error) {
if (error) return resp.end('Resp Error :', error.message);
resp.pipe(file);
}).on('error', function (e) {
console.log(e)
}).end();
// this is the classic api
file
.on('data', function (data) { console.log('Data!', data); })
.on('error', function (err) { console.error('Error', err); })
.on('end', function () { console.log('All done!'); });
}
/*
// Method 2 :
// Note : Getting error in writing file to local disk while behind VPN
// ==============================
var Stream = require('stream').Transform;
var downloadImageToUrl = (url, filename, callback) => {
var client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.request(url, function (response) {
var data = new Stream();
response.on('data', function (chunk) {
data.push(chunk);
});
response.on('end', function () {
fs.writeFileSync(filename, data.read());
});
}).end();
};
*/
console.log(`Server listening on port http://localhost:${port}`);