简体   繁体   English

在SailsJs中同时开发Rest API和服务器端渲染

[英]Develop Rest API and server-side rendering simultaneously in SailsJs

I am looking towards the SailsJs framework for nodeJs to build an app. 我正在寻找用于nodeJs的SailsJs框架来构建应用程序。

Initially I planned to build just a REST API in sails and manage the Front-End using AngularJs, the API would also be useful for other non-browser apps. 最初,我计划仅构建一个REST API并使用AngularJs管理前端,该API对其他非浏览器应用程序也将很有用。 But then I realized since angularJs is client side data binding , My site might not be SEO friendly and IE-8 compatible. 但是后来我意识到,由于angularJs是客户端数据绑定,所以我的站点可能不兼容SEO且不兼容IE-8。

So what I am trying to find out now is that : 所以我现在想找出的是:

Is it possible to develop a site which uses server-side(ejs) rendering and together with that, develop a REST API simultaneously with the least efforts. 是否有可能开发一个使用服务器端(ejs)渲染的站点,并与此同时,以最少的努力同时开发REST API。 and if I go this way, What things one should keep in mind. 如果我这样走,应该记住什么。

Thanks. 谢谢。

This is totally possible! 这完全有可能! Take a look at req.wantsJSON . 看看req.wantsJSON

A flag indicating whether the requesting client would prefer a JSON response (as opposed to some other format, like XML or HTML.) 一个标志,指示发出请求的客户端是否希望使用JSON响应(与XML或HTML等其他格式相对)。

The best point about this is that: 最好的一点是:

req.wantsJSON is used by all of the built-in custom responses in Sails. Sails中的所有内置自定义响应都使用req.wantsJSON。

It means that you have nothing to do server-side for it to work! 这意味着您无需做任何服务器端工作!

You only have to create the view corresponding to the action (usually find or findOne) and watch out for the headers you send to your Sails server. 您只需创建与操作相对应的视图(通常为find或findOne),并注意发送给Sails服务器的标头。


For example, let's generate an "api" (controller/model) with Sails Command Line Interface 例如,让我们使用Sails命令行界面生成一个“ api”(控制器/模型)

$ sails generate api product

Now let's create some products with the Sails Console 现在,让我们使用Sails Console创建一些产品

$ sails console

sails> Product.create({name: 'Blue chair'}).exec(console.log)
undefined
sails> null { name: 'Blue chair',
 createdAt: "2014-12-30T04:29:15.447Z",
 updatedAt: "2014-12-30T04:29:15.447Z",
 id: 1 }

sails> Product.create({name: 'Red table'}).exec(console.log)
undefined
sails> null { name: 'Red table',
 createdAt: "2014-12-30T04:29:25.447Z",
 updatedAt: "2014-12-30T04:29:25.447Z",
 id: 2 }

sails> Product.create({name: 'Yellow hammer'}).exec(console.log)
undefined
sails> null { name: 'Yellow hammer',
 createdAt: "2014-12-30T04:29:35.447Z",
 updatedAt: "2014-12-30T04:29:35.447Z",
 id: 3 }

If you lift your sails app right now and access http: //localhost:1337/product, you should have something like 如果您立即举起帆应用程序并访问http:// localhost:1337 / product,则应该有类似以下内容

 [
  {
   name: 'Blue chair',
   createdAt: "2014-12-30T04:29:15.447Z",
   updatedAt: "2014-12-30T04:29:15.447Z",
   id: 1
  },
  {
   name: 'Red table',
   createdAt: "2014-12-30T04:29:25.447Z",
   updatedAt: "2014-12-30T04:29:25.447Z",
   id: 2
  },
  {
   name: 'Yellow hammer',
   createdAt: "2014-12-30T04:29:35.447Z",
   updatedAt: "2014-12-30T04:29:35.447Z",
   id: 3
  }
 ]

Which is JSON (you can check the Content-Type header value in the Response Headers of the Network section in your browser Developer Tools). 这是JSON(您可以在浏览器开发人员工具的“网络”的“响应标头”部分中检查Content-Type标头值)。

To associate a view to your resource, you'll need to create a folder named with the name of your resource in your views folder, and create file named with the name of the action (find or findOne usually). 要将视图与资源相关联,您需要在views文件夹中创建一个以资源名称命名的文件夹,并创建以操作名称命名的文件(通常为find或findOne)。

If you refresh the page in your browser, you should see what's inside your view (nothing basically). 如果在浏览器中刷新页面,则应该看到视图内部的内容(基本上没有任何内容)。 So let's create the "find" view which contains the list of products 因此,让我们创建一个包含产品列表的“查找”视图

<ul>
    <% _.each(data, function (product) { %>
    <li><%= product.name %></li>
    <% }) %>
</ul>

In your browser, you should have something like : 在浏览器中,您应该有类似以下内容:

  • Blue chair 蓝色椅子
  • Red table 红表
  • Yellow hammer 黄锤

Now, how can you get the JSON data for your other client app? 现在,如何获取其他客户端应用程序的JSON数据? Just by giving the correct HTTP Headers. 只需提供正确的HTTP标头即可。

As said in the req.wantsJSON doc 如在req.wantsJSON文档中所述

if this request has a "json" content-type AND ALSO has its "Accept" header set 如果此请求具有“ json”内容类型,并且还设置了“ Accept”标头

You have to set both the "Content-Type" and the "Accept" headers in your HTTP request. 您必须在HTTP请求中同时设置“ Content-Type”和“ Accept”标头。 By testing it, I found out that the "Accept" header is sufficient. 通过测试,我发现“ Accept”标头就足够了。 So you only have to set one these values to this header : 因此,您只需将以下值之一设置为此标头:

  • "application/json" if you want JSON 如果需要JSON,则为“ application / json”
  • "text/html" if you want your view 如果您想查看“ text / html”

And that's it! 就是这样!


I hope I didn't forget anything. 我希望我不会忘记任何事情。 Do not hesitate if you need more precision! 如果您需要更高的精度,请不要犹豫!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM