examples/resource/index.js JAVASCRIPT 96 lines View on github.com → Search inside
1'use strict'23/**4 * Module dependencies.5 */67var express = require('../../');89var app = module.exports = express();1011// Ad-hoc example resource method1213app.resource = function(path, obj) {14  this.get(path, obj.index);15  this.get(path + '/:a..:b{.:format}', function(req, res){16    var a = parseInt(req.params.a, 10);17    var b = parseInt(req.params.b, 10);18    var format = req.params.format;19    obj.range(req, res, a, b, format);20  });21  this.get(path + '/:id', obj.show);22  this.delete(path + '/:id', function(req, res){23    var id = parseInt(req.params.id, 10);24    obj.destroy(req, res, id);25  });26};2728// Fake records2930var users = [31  { name: 'tj' }32  , { name: 'ciaran' }33  , { name: 'aaron' }34  , { name: 'guillermo' }35  , { name: 'simon' }36  , { name: 'tobi' }37];3839// Fake controller.4041var User = {42  index: function(req, res){43    res.send(users);44  },45  show: function(req, res){46    res.send(users[req.params.id] || { error: 'Cannot find user' });47  },48  destroy: function(req, res, id){49    var destroyed = id in users;50    delete users[id];51    res.send(destroyed ? 'destroyed' : 'Cannot find user');52  },53  range: function(req, res, a, b, format){54    var range = users.slice(a, b + 1);55    switch (format) {56      case 'json':57        res.send(range);58        break;59      case 'html':60      default:61        var html = '<ul>' + range.map(function(user){62          return '<li>' + user.name + '</li>';63        }).join('\n') + '</ul>';64        res.send(html);65        break;66    }67  }68};6970// curl http://localhost:3000/users     -- responds with all users71// curl http://localhost:3000/users/1   -- responds with user 172// curl http://localhost:3000/users/4   -- responds with error73// curl http://localhost:3000/users/1..3 -- responds with several users74// curl -X DELETE http://localhost:3000/users/1  -- deletes the user7576app.resource('/users', User);7778app.get('/', function(req, res){79  res.send([80    '<h1>Examples:</h1> <ul>'81    , '<li>GET /users</li>'82    , '<li>GET /users/1</li>'83    , '<li>GET /users/3</li>'84    , '<li>GET /users/1..3</li>'85    , '<li>GET /users/1..3.json</li>'86    , '<li>DELETE /users/4</li>'87    , '</ul>'88  ].join('\n'));89});9091/* istanbul ignore next */92if (!module.parent) {93  app.listen(3000);94  console.log('Express started on port 3000');95}

Code quality findings 12

Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var express = require('../../');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var app = module.exports = express();
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var a = parseInt(req.params.a, 10);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var b = parseInt(req.params.b, 10);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var format = req.params.format;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var id = parseInt(req.params.id, 10);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var users = [
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var User = {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var destroyed = id in users;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var range = users.slice(a, b + 1);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var html = '<ul>' + range.map(function(user){
Remove debugging statements or use a logging library
info correctness console-log
console.log('Express started on port 3000');

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.