Skip to content

Commit ba7c39c

Browse files
author
Andrey
committed
Rewrote the server with express
1 parent fae50ba commit ba7c39c

File tree

3 files changed

+448
-63
lines changed

3 files changed

+448
-63
lines changed

index.js

Lines changed: 34 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,39 @@
1-
const http = require('http');
2-
const url = require('url');
1+
const express = require('express');
32
const fs = require('fs');
43
const path = require('path');
54
const mimeType = require('./modules/mimeType');
65
const port = 9000;
76

8-
http
9-
.createServer(function (req, res) {
10-
console.log(`${req.method} ${req.url}`);
11-
12-
// parse URL
13-
const parsedUrl = url.parse(req.url);
14-
const sanitizePath = path
15-
.normalize(parsedUrl.pathname)
16-
.replace(/^(\.\.[\/\\])+/, '');
17-
let pathname = path.join(__dirname, sanitizePath);
18-
19-
fs.exists(pathname, function (exist) {
20-
if (!exist) {
21-
// if the file is not found, return 404
22-
res.statusCode = 404;
23-
res.end(`File ${pathname} not found!`);
24-
return;
25-
}
26-
27-
// if is a directory return the list of files
28-
if (fs.statSync(pathname).isDirectory()) {
29-
fs.readdir(pathname, function (err, files) {
30-
//handling error
31-
if (err) {
32-
return console.log('Unable to scan directory: ' + err);
33-
}
34-
35-
var index = pathname.lastIndexOf('/');
36-
var reqPath = pathname.substring(index + 1);
37-
38-
if (reqPath === 'files') {
39-
res.setHeader('Content-type', mimeType['.json'])
40-
res.end(JSON.stringify(files));
41-
} else {
42-
res.writeHead(404, {
43-
'Content-type': 'text/html'
44-
});
45-
res.end(`<h1>Incorrect Request</h1>`);
46-
}
47-
48-
});
49-
}
50-
51-
// read file from file system
52-
fs.readFile(pathname, function (err, data) {
53-
if (err) {
54-
res.statusCode = 500;
55-
res.end(`Error getting the file: ${err}.`);
56-
} else {
57-
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
58-
const ext = path.parse(pathname).ext;
59-
// if the file is found, set Content-type and send data
60-
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
61-
res.end(data);
62-
}
63-
});
64-
});
65-
})
66-
.listen(parseInt(port));
67-
68-
console.log(`Server listening on port ${port}`);
7+
const app = express();
8+
9+
app.get('/files', (req, res) => {
10+
fs.readdir('./files', function (err, files) {
11+
if (err) {
12+
return console.log('Unable to scan directory: ' + err);
13+
}
14+
res.setHeader('Content-type', mimeType['.json']);
15+
res.end(JSON.stringify(files));
16+
});
17+
});
18+
19+
app.get('/files/:fileName', (req, res) => {
20+
const pathname = `./files/${req.params.fileName}`;
21+
fs.readFile(pathname, function (err, data) {
22+
if (err) {
23+
res.statusCode = 500;
24+
res.end(`Error getting the file: ${err}.`);
25+
} else {
26+
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
27+
const ext = path.parse(pathname).ext;
28+
// if the file is found, set Content-type and send data
29+
res.setHeader('Content-type', mimeType[ext] || 'text/plain');
30+
res.end(data);
31+
}
32+
});
33+
});
34+
35+
app.listen(port, () =>
36+
console.log(
37+
`nodejs-convert-file-server listening at http://localhost:${port}`,
38+
),
39+
);

0 commit comments

Comments
 (0)