blai / grunt-express

Start an Express.js web server using grunt.js
MIT License
254 stars 41 forks source link

Creating an API - integrating with node-restful (https://github.com/baugarten/node-restful) #45

Open iladarsda opened 10 years ago

iladarsda commented 10 years ago

Is it possible to make grunt-express work with some 'classic' express extensions to create an API; e.g. node-restful APIs (https://github.com/baugarten/node-restful)?

For example below is what my current app.js file looks file, but this would not work when loaded through script: '/path/to/app.js

Any suggestion much appreciated.

var express = require('express'),
  path = require("path"),
  application_root = __dirname,
  restful = require('node-restful'),
  mongoose = restful.mongoose;

var app = express();
app.configure(function() {

  app.use(express.json());
  app.use(express.urlencoded());
  app.use(express.methodOverride());
  app.use(express.bodyParser());
  app.use(app.router);
  app.use(express.query());
  app.use(express.static(path.join(application_root, "/public")));
  app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
  }));

});

app.all('*', function(req, res, next) {

  if (!req.get('Origin')) return next();
  res.set('Access-Control-Allow-Origin', "*");
  res.set('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
  res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
  if ('OPTIONS' == req.method) return res.send(200);
  next();

});

mongoose.connect("mongodb://localhost:27017/testdb");

var Resource = app.item = restful.model('item', mongoose.Schema({
  tlt: 'String',
  name: 'String',
})).methods(['get', 'post', 'put', 'delete']);
Resource.register(app, '/api/item');

app.listen(9000);