@ndiinginc/router 4.16.1 → 4.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/response.js +9 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndiinginc/router",
3
- "version": "4.16.1",
3
+ "version": "4.16.2",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/response.js CHANGED
@@ -16,10 +16,8 @@ class Response extends http.ServerResponse {
16
16
  * @param {string} data
17
17
  */
18
18
  send(data) {
19
- this.writeHead(this.statusCode, {
20
- "Content-Type": "text/html",
21
- "Content-Length": Buffer.byteLength(data),
22
- });
19
+ this.setHeader("Content-Type", "text/html");
20
+ this.setHeader("Content-Length", Buffer.byteLength(data));
23
21
  this.end(data);
24
22
  }
25
23
 
@@ -28,10 +26,8 @@ class Response extends http.ServerResponse {
28
26
  */
29
27
  json(data) {
30
28
  const body = JSON.stringify(data);
31
- this.writeHead(this.statusCode, {
32
- "Content-Type": "application/json",
33
- "Content-Length": Buffer.byteLength(body),
34
- });
29
+ this.setHeader("Content-Type", "application/json");
30
+ this.setHeader("Content-Length", Buffer.byteLength(body));
35
31
  this.end(body);
36
32
  }
37
33
 
@@ -41,23 +37,21 @@ class Response extends http.ServerResponse {
41
37
  */
42
38
  download(filePath, filename) {
43
39
  if (!fs.existsSync(filePath)) {
44
- this.writeHead(404, { "Content-Type": "text/plain" });
40
+ this.status(404);
45
41
  return this.end();
46
42
  }
47
43
 
48
44
  const stat = fs.statSync(filePath);
49
45
  const basename = filename || path.basename(filePath);
50
46
 
51
- this.writeHead(200, {
52
- "Content-Type": "application/octet-stream",
53
- "Content-Length": stat.size,
54
- "Content-Disposition": `attachment; filename="${basename}"`,
55
- });
47
+ this.setHeader("Content-Type", "application/octet-stream");
48
+ this.setHeader("Content-Length", stat.size);
49
+ this.setHeader("Content-Disposition", `attachment; filename="${basename}"`);
56
50
 
57
51
  const stream = fs.createReadStream(filePath);
58
52
  stream.on("error", (err) => {
59
53
  if (!this.headersSent) {
60
- this.writeHead(500, { "Content-Type": "text/plain" });
54
+ this.status(500);
61
55
  }
62
56
  this.end();
63
57
  });