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

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

问题描述

我是 Node 的初学者(通常都是后端 Web 开发),并且我已经开始在 Node 中编写 RESTful API.有几件事我想弄清楚.

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.

我的应用程序使用 Express 和 Mongoose,我正在使用 express-resource 模块轻松地为 API 资源创建我的 CRUD 路由.但是有几件事我不满意,我认为我可以做得更好.

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.

第一个是猫鼬.如果我想为我的 API 编写测试,我无法通过 stub Mongoose 将其强制写入内存数据.然而,那里的所有教程似乎都指向 Mongoose,而且我真的不确定我应该使用什么.

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.

其次,我的资源似乎有很多样板代码.这真的是在 Node.js 中创建 RESTful API 的最佳方式吗?是否有其他模块可以帮助我创建 CRUD 路由?我相信有一些方法可以让您直接从您的架构创建 CRUD 路由,而不再需要代码,但我真的不确定如何.

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.

我已经看到了诸如 Tower.js 和 CompoundJS(正式的 RailJS)之类的项目,它们似乎是这些综合解决方案,解决的问题比我在这里的问题要多得多.也许我应该使用它们,但我真的只希望 Node.js 应用程序是一个 API,仅此而已.我独立于 API 处理前端.

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.

为了提供一些背景信息,这是我目前的情况.目前,我在 Mongoose 中定义了一个模型:

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')

接下来,我为 CRUD 路由定义控制器:

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()
  })
}

最后,我使用 express-resource 模块将这些控制器映射到 Express 应用顶部的必要 CRUD 路由.

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'))

推荐答案

你应该研究一下 restify

如果你想用express,你也可以看看我做的这个项目——叫做node-安静.

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

这个库似乎更加成熟并且具有更多功能:https://github.com/jspears/鱼

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天全站免登陆