如何最好地创建Node.js的一个RESTful API [英] How to best create a RESTful API in Node.js

查看:168
本文介绍了如何最好地创建Node.js的一个RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在节点(通常所有后端的Web开发)一个初学者,我已经开始写节点中的一个RESTful API。还有我想围绕我的头几件事情。

我的应用程序使用前preSS和猫鼬,我现在用的是前preSS-资源模块能够轻松地创建我的API资源的CRUD路线。但也有几件事情我很不满意,并认为我可以做的更好。

首先是猫鼬。如果我想写测试我的API,我没有存根猫鼬在内存中的数据是强制的方式。所有的教程在那里的似乎指向然而,猫鼬,和我真的不知道我应该使用。

其次,我国的资源似乎有很多样板code的。这真的是创造Node.js的一个RESTful API的最佳方式?是否有其他模块,这将帮助我创造我的CRUD路线?我相信,有办法,你可以直接从您的方案中创建CRUD路线,没有了code,但我真的不知道如何。

我见过的项目在那里,如Tower.js和CompoundJS(正式RailwayJS),似乎是这些COM prehensive解决方案,以解决比这里我的问题更多。也许我应该使用他们,但我真的只是想Node.js的应用程序是一个API,仅此而已。我处理的前端独立的API。

要提供一些背景,这是我目前的状况。目前,我在猫鼬定义的模型:

  VAR猫鼬=要求('猫鼬')
  ,架构= mongoose.Schema
  ,链接VAR LinkSchema =新模式({
  URI:字符串,
  元:{
    标题:字符串,
    说明:字符串
  },
  分享: [{
    UID:Schema.Types.ObjectId,
    日期:日期,
    消息:字符串
  }]
})链接= module.exports = mongoose.model(链接)

接下来,我定义控制器的CRUD路线:

  VAR猫鼬=要求('猫鼬')
  ,_ =要求('下划线')
  ,链接= mongoose.model(链接)exports.load =功能(REQ,ID,FN){
  Link.findById(req.params.link,函数(ERR,链接){
    如果(ERR){
      返回res.send(ERR)
    }    FN(NULL,链接)
  })
}exports.index =功能(REQ,RES){
  VAR filterByUser = req.query.user? {'shares.uid':req.query.user}:{}  Link.find(filterByUser,函数(ERR,链接){
    如果(ERR){
      返回res.send(ERR)
    }    res.send(链接)
  })
}exports.create =功能(REQ,RES){
  VAR链接=新的链接(req.body)  link.save(功能(错误){
    如果(ERR){
      // TODO:送404
      返回res.send(ERR)
    }    res.send(链接)
  })
}exports.show =功能(REQ,RES){
  res.send(req.link)
}exports.update =功能(REQ,RES){
  req.link = _(req.link).extend(req.body)  req.link.save(函数(ERR,链接){
    如果(ERR){
      返回res.send(ERR)
    }    res.send(链接)
  })
}exports.patch = exports.updateexports.destroy =功能(REQ,RES){
  req.link.remove(功能(错误){
    如果(ERR){
      返回res.send(ERR)
    }    res.send()
  })
}

最后,我用的是前preSS-资源模块的防爆preSS应用程序的顶部这些控制器必要的CRUD路线图。

  app.resource(API /链接,需要('../资源/链接'))


解决方案

您应该看看的RESTify

如果你想使用前preSS,你也可以看看这个项目,我做了 - 叫节点宁静

该库似乎更加成熟,有更多的功能,但: https://github.com/jspears/mers

I'm a beginner in Node (and generally all back-end web development), and I have started to write a RESTful API in Node. There are a few things I'm trying to get my head around.

My application uses Express and Mongoose, and I am using the express-resource module to easily create my CRUD routes for the API resources. But there are a couple of things I am unhappy about, and think I could do better.

The first is Mongoose. If I want to write tests for my API, I have no way of stubbing Mongoose to force it to in memory data. All of the tutorials out there seem to point to Mongoose, however, and I'm really not sure what I should be using.

Secondly, my resource seems to have a lot of boilerplate code. Is this really the best way to create a RESTful API in Node.js? Are there other modules that will help me to create my CRUD routes? I believe there are ways you can create CRUD routes right from your schema, without anymore code, but I'm really not sure how.

I have seen projects out there such as Tower.js and CompoundJS (formally RailwayJS) that seem to be these comprehensive solutions that solve much more than my issues here. Perhaps I should be using them, but I really only want the Node.js application to be an API and nothing more. I am dealing with the front-end independently of the API.

To provide some context, here is my current situation. Currently, I have a model defined in Mongoose:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema
  , Link

var LinkSchema = new Schema({
  uri: String,
  meta: {
    title: String,
    desc: String
  },
  shares: [{
    uid: Schema.Types.ObjectId,
    date: Date,
    message: String
  }]
})

Link = module.exports = mongoose.model('Link')

Next, I define the controllers for the CRUD routes:

var mongoose = require('mongoose')
  , _ = require('underscore')
  , Link = mongoose.model('Link')

exports.load = function (req, id, fn) {
  Link.findById(req.params.link, function (err, link) {
    if (err) {
      return res.send(err)
    }

    fn(null, link)
  })
}

exports.index = function (req, res) {
  var filterByUser = req.query.user ? { 'shares.uid': req.query.user } : {}

  Link.find(filterByUser, function (err, links) {
    if (err) {
      return res.send(err)
    }

    res.send(links)
  })
}

exports.create = function (req, res) {
  var link = new Link(req.body)

  link.save(function (err) {
    if (err) {
      // TODO: send 404
      return res.send(err)
    }

    res.send(link)
  })
}

exports.show = function (req, res) {
  res.send(req.link)
}

exports.update = function (req, res) {
  req.link = _(req.link).extend(req.body)

  req.link.save(function (err, link) {
    if (err) {
      return res.send(err)
    }

    res.send(link)
  })
}

exports.patch = exports.update

exports.destroy = function (req, res) {
  req.link.remove(function (err) {
    if (err) {
      return res.send(err)
    }

    res.send()
  })
}

Finally, I use the express-resource module to map these controllers to the necessary CRUD routes on top of the Express app.

app.resource('api/links', require('../resources/links'))

解决方案

You should look into restify

If you want to use express, you can also check out this project that I made -- called node-restful.

This library seems to be much more mature and have more features though: https://github.com/jspears/mers

这篇关于如何最好地创建Node.js的一个RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆