Browse Source

added res.json() function

master
mortie 7 years ago
parent
commit
8b413a4269
3 changed files with 14 additions and 1 deletions
  1. 5
    0
      README.md
  2. 8
    0
      index.js
  3. 1
    1
      package.json

+ 5
- 0
README.md View File

@@ -90,6 +90,11 @@ In the future, it will be possible to control the logging level.

In the future, there will be middleware for POST request too.

### Modifications to the request and response objects

* `req.urlobj`: The result of `url.parse(req.url)`.
* `res.json(object)`: A utility function to respond with JSON.

### webframe.static(root[, before])

Serve static files.

+ 8
- 0
index.js View File

@@ -12,6 +12,13 @@ function template(tpml, args) {
return tpml;
}

function resJson(obj) {
res.writeHead(200, {
"content-type": "application/json"
});
this.end(JSON.stringify(obj));
}

class App {
constructor(options) {
options = options || {};
@@ -55,6 +62,7 @@ class App {

// Listen for requests
this.server.on("request", (req, res) => {
res.json = resJson;
var url = urllib.parse(req.url);
req.urlobj = url;


+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
{
"name": "webframe",
"version": "0.1.4",
"version": "0.2.0",
"description": "Web server.",
"main": "index.js",
"scripts": {

Loading…
Cancel
Save