-
Notifications
You must be signed in to change notification settings - Fork 415
solution #365
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 #365
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
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,95 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = http.createServer((req, res) => { | ||
| const rawUrl = req.url; | ||
| const urlPath = rawUrl.split('?')[0]; | ||
|
|
||
| if (urlPath.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (urlPath.startsWith('/file/')) { | ||
| const relativeRaw = urlPath.slice('/file/'.length); | ||
|
|
||
| let relative; | ||
|
|
||
| try { | ||
| relative = decodeURIComponent(relativeRaw); | ||
| } catch { | ||
| res.statusCode = 400; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
Comment on lines
+27
to
+30
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. In the |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (relative === '') { | ||
| relative = 'index.html'; | ||
| } else if (relative.endsWith('/')) { | ||
| relative = `${relative}index.html`; | ||
| } | ||
|
|
||
| const publicDir = path.resolve(__dirname, '../public'); | ||
| const filePath = path.resolve(publicDir, relative); | ||
|
|
||
| const relativePath = path.relative(publicDir, filePath); | ||
|
|
||
| if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) { | ||
| res.statusCode = 400; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.readFile(filePath, (err, data) => { | ||
| if (err) { | ||
| res.statusCode = 404; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 200; | ||
|
|
||
| const extension = path.extname(filePath); | ||
|
|
||
| if (extension === '.html') { | ||
| res.setHeader('content-type', 'text/html'); | ||
| } else if (extension === '.css') { | ||
| res.setHeader('content-type', 'text/css'); | ||
| } | ||
|
|
||
| res.end(data); | ||
|
Comment on lines
+63
to
+73
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. On successful file reads you set |
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (urlPath !== '/file') { | ||
| res.statusCode = 400; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
|
||
| return; | ||
|
Comment on lines
+79
to
+84
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. This branch handles requests not equal to
Comment on lines
+79
to
+84
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. This block returns |
||
| } | ||
|
Comment on lines
+78
to
+85
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. This block returns a 400 for any request whose pathname is not exactly |
||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Use /file/<filename> to serve a file from the public folder'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| 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.
When the request is
/file/therelativeRawwill be an empty string; per requirements you must returnpublic/index.html. Right now the code will try to read the directory (and likely return 404). Detect an emptyrelative(or a trailing/) and useindex.htmlas the target file.