-
Notifications
You must be signed in to change notification settings - Fork 404
Solution #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #346
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,45 @@ | ||
| 'use strict'; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| } | ||
| const http = require('http'); | ||
| const path = require('path'); | ||
| const fsp = require('fs/promises'); | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| const createServer = () => { | ||
| return http.createServer(async (req, res) => { | ||
| const url = new URL(req.url || '', `http://${req.headers.host}`); | ||
| const regexPattern = /^\/file(?:\/|$)/; | ||
| const isValidURL = regexPattern.test(url.pathname); | ||
|
|
||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| if (!isValidURL) { | ||
| res.statusCode = 400; | ||
| res.end('Invalid URL. Correct request is: "/file/<PATH_TO_THE_FILE>".'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const requestedPath = | ||
| url.pathname.replace(regexPattern, '') || 'index.html'; | ||
|
|
||
| const publicDir = path.resolve('public'); | ||
| const realPath = path.resolve(publicDir, requestedPath); | ||
|
|
||
| if (path.relative(publicDir, realPath).slice(0, 2) === '..') { | ||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After sending the response here, you need to add |
||
| } | ||
|
|
||
| try { | ||
| const file = await fsp.readFile(realPath, 'utf-8'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading files with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifying the |
||
|
|
||
| res.statusCode = 200; | ||
| res.end(file); | ||
| } catch (error) { | ||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
|
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
| } | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = { createServer }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code sets
Content-Typetotext/plainfor every response. For better behavior set MIME types according to the file extension (e.g.text/htmlfor.html) or at minimum settext/htmlforindex.htmlresponses.