-
Notifications
You must be signed in to change notification settings - Fork 415
Solution #370
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 #370
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,68 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const filePath = req.url; | ||
|
|
||
| if (filePath.includes('..')) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('path should not contain ..'); | ||
|
|
||
| return; | ||
| } else if (filePath.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Invalid path'); | ||
|
|
||
| return; | ||
| } else if (filePath === '/file') { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| res.end( | ||
| 'path to file should start with /file/ and include the file name', | ||
| ); | ||
|
|
||
| return; | ||
| } else if (!filePath.startsWith('/file/')) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('path to file should start with /file/'); | ||
|
Comment on lines
+33
to
+36
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 branch for requests that don't start with
Comment on lines
+33
to
+36
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 branch for paths that do not start with |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| const relativePath = filePath.slice('/file/'.length); | ||
| let fileData; | ||
|
|
||
| try { | ||
| if (relativePath === '') { | ||
| fileData = fs.readFileSync('./public/index.html'); | ||
| } else { | ||
| fileData = fs.readFileSync(`./public/${relativePath}`); | ||
| } | ||
| } catch (error) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('File not found'); | ||
|
|
||
| return; | ||
| } | ||
| res.statusCode = 200; | ||
|
|
||
| if (filePath.endsWith('.html')) { | ||
| res.setHeader('Content-Type', 'text/html'); | ||
| } else if (filePath.endsWith('.css')) { | ||
| res.setHeader('Content-Type', 'text/css'); | ||
| } | ||
| res.end(fileData); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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.
Per the requirements, any request whose pathname does NOT start with
/file/must return the hint message with status code 200 andContent-Type: text/plain. Here you setres.statusCode = 400. Change this to200so the response matches the spec and tests.