这Node.js代码片段有什么问题?

huangapple go评论93阅读模式
英文:

What is wrong with this Node.js snippet?

问题

以下是您要翻译的代码部分:

  1. const fs = require('fs');
  2. const http = require('http');
  3. const server = http.createServer((req, res) => {
  4. if (req.headers['x-secret'] != process.env.SECRET )
  5. res.writeHead(403).end('Secret incorrect');
  6. let body = [];
  7. req.on('data', chunk => {
  8. body.push(chunk);
  9. });
  10. req.on('end', () => {
  11. body = JSON.parse(Buffer.concat(body).toString());
  12. fs.writeFileSync(body.filename, body.file);
  13. res.writeHead(200).end('OK');
  14. });
  15. });
  16. server.listen(7654);

请注意,我已将 HTML 实体编码转换回正常的 JavaScript 代码。

英文:

I am learning node.js and the course says there is something wrong with the following snippet.

  1. const fs = require('fs');
  2. const http = require('http');
  3. const server = http.createServer((req, res) => {
  4. if (req.headers['x-secret'] != process.env.SECRET )
  5. res.writeHead(403).end('Secret incorrect');
  6. let body = [];
  7. req.on('data', chunk => {
  8. body.push(chunk);
  9. });
  10. req.on('end', () => {
  11. body = JSON.parse(Buffer.concat(body).toString());
  12. fs.writeFileSync(body.filename, body.file);
  13. res.writeHead(200).end('OK');
  14. });
  15. });
  16. server.listen(7654);

Possible things I've found include:

  • https should be used instead of http (secure server)

  • Res.writeHead.end is not valid syntax. Res.writeHead and res.end
    should be written separately

  • fs.writeFile() should be used, not the async version

  • There's no failsafe built-in (?)

However the course seems to be saying that there's a big mistake, which I can't really find.

Please help!

Thanks

答案1

得分: 1

Buffer.concat(body).toString()
不是有效的JSON,所以你无法解析它。
如果你将其记录下来,你将会收到以下内容:

  1. ----------------------------118769234111712879210320
  2. Content-Disposition: form-data; name="data"; filename="test.json"
  3. Content-Type: application/json
  4. {
  5. "test": "156"
  6. }
  7. ----------------------------118769234111712879210320--

像这样。

英文:

Buffer.concat(body).toString()
is not valid JSON, so You can't parse it.
what you will receive if you log it

  1. ----------------------------118769234111712879210320
  2. Content-Disposition: form-data; name="data"; filename="test.json"
  3. Content-Type: application/json
  4. {
  5. "test": "156"
  6. }
  7. ----------------------------118769234111712879210320--

like this

答案2

得分: 0

我只看到一个问题在这里,就是在这一行:

  1. let body = [];

你不能将body初始化为空数组,因为你在下面的这一行中将它用作对象:

  1. fs.writeFileSync(body.filename, body.file);

你不能使用body.filename,因为点符号只用于对象。

英文:

The only problem i see here is in the line

  1. let body = [];

you cannot initialize body as an empty array as you are using it as an object in line

  1. fs.writeFileSync(body.filename, body.file);

you cannot do body.filename as dot notation is only used in an object.

huangapple
  • 本文由 发表于 2023年1月8日 08:27:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75044721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定