Permalink
Browse files

Update example to use latest router API from connect 0.2.3

  • Loading branch information...
1 parent 135527c commit d4fd940e58e219353a68086f675feed67c8dbf8c @creationix committed Jul 28, 2010
Showing with 48 additions and 47 deletions.
  1. +3 −3 articles/deploying-node-with-spark/config.js
  2. +45 −44 articles/deploying-node-with-spark/memory_bank.js
@@ -1,5 +1,5 @@
module.exports = {
- port: 80,
- user: "nobody",
- env: "production"
+ port: 80,
+ user: "nobody",
+ env: "production"
};
@@ -1,56 +1,57 @@
//routes
module.exports = function (app) {
-
- // Read a value from the database
- app.get("/:key", function (req, res, params, next) {
- // Load from the database
- var item = data[params.key];
- // 404 if it doesn't exist
- if (!item) { next(); return; }
- // Serve the item to the client
- sendItem(res, item);
- });
-
- // Save a value to the database
- app.put("/:key", function (req, res, params, next) {
- // Create/insert the item in the database
- var item = data[params.key] = {
- value: req.body,
- mtime: new Date
- };
- // Serve the item to the client
- sendItem(res, item);
- });
-
- // Remove a value from the database
- app.del("/:key", function (req, res, params, next) {
- // Load from the database
- var item = data[params.key];
- // 404 if it doesn't exist
- if (!item) { next(); return; }
- // Delete it
- delete data[params.key];
- // Send an empty OK response
- res.writeHead(204, {});
- res.end();
- });
+
+ // Read a value from the database
+ app.get("/:key", function (req, res, next) {
+ // Load from the database
+ var item = data[req.params.key];
+ // 404 if it doesn't exist
+ if (!item) { next(); return; }
+ // Serve the item to the client
+ sendItem(res, item);
+ });
+
+ // Save a value to the database
+ app.put("/:key", function (req, res, next) {
+ // Create/insert the item in the database
+ var item = data[req.params.key] = {
+ value: req.body,
+ mtime: new Date
+ };
+ // Serve the item to the client
+ sendItem(res, item);
+ });
+
+ // Remove a value from the database
+ app.del("/:key", function (req, res, next) {
+ var key = req.params.key;
+ // Load from the database
+ var item = data[key];
+ // 404 if it doesn't exist
+ if (!item) { next(); return; }
+ // Delete it
+ delete data[key];
+ // Send an empty OK response
+ res.writeHead(204, {});
+ res.end();
+ });
};
//rest
// Simplest database ever!
var data = {
- greeting: {
- value: {hello: "world"},
- mtime: new Date
- }
+ greeting: {
+ value: {hello: "world"},
+ mtime: new Date
+ }
};
// Helper for sending items
function sendItem(res, item) {
- // Send the response
- res.writeHead(200, {
- "Content-Type": "application/json",
- "Last-Modified": item.mtime.toUTCString()
- });
- res.end(JSON.stringify(item.value));
+ // Send the response
+ res.writeHead(200, {
+ "Content-Type": "application/json",
+ "Last-Modified": item.mtime.toUTCString()
+ });
+ res.end(JSON.stringify(item.value));
}

0 comments on commit d4fd940

Please sign in to comment.