简体   繁体   English

Sails.js如何为Yeoman生成的应用程序提供服务

[英]How can Sails.js serve a Yeoman generated app

I am new to Sails.js, and I'm pretty excited by it. 我是Sails.js的新手,我对它非常兴奋。 It is a great tool for creating APIs. 它是创建API的绝佳工具。 I am also working for a while with Yeoman generated single page apps (jquery or angular) and they are great for the client side logic. 我也在Yeoman工作了一段时间生成单页应用程序(jquery或angular),它们非常适合客户端逻辑。

However, currently the only way to make an app based on the two technologies is to separate them into two completely separate projects, which is wasteful. 但是,目前基于这两种技术制作应用程序的唯一方法是将它们分成两个完全独立的项目,这是浪费的。

I am looking for a way to combine the two. 我正在寻找一种将两者结合起来的方法。 I want to make Sails.js serve the static assets of the web app and load its index.html file (instead of homepage.ejs). 我想让Sails.js服务于Web应用程序的静态资产并加载其index.html文件(而不是homepage.ejs)。 I also want the development environment (grunt, live-reload to work as it should). 我也想要开发环境(grunt,live-reload可以正常工作)。

I've tried creating a yeoman project inside the assets folder and redirecting the layout to the index.html but it has several issues: - I cannot separate between the app/ folder (during development) and the dist/ folder (in production) - the bower_components link is broken (it refers to /bower_components instead of /assets/app/bower_components) 我已经尝试在assets文件夹中创建一个yeoman项目并将布局重定向到index.html但它有几个问题: - 我无法在app /文件夹(开发期间)和dist /文件夹(生产中)之间分开 - bower_components链接被破坏(它指的是/ bower_components而不是/ assets / app / bower_components)

I guess there might be more issues I haven't discovered yet. 我想可能还有更多我尚未发现的问题。

Has anyone tried (and succeeded) combining these technologies? 有没有人尝试(并成功)结合这些技术?

You can serve the static index.html instead of homepage.ejs by doing the steps below: 您可以通过执行以下步骤来提供静态index.html而不是homepage.ejs:

  1. place your index.html file under the assets folder. 将index.html文件放在assets文件夹下。
  2. open routes.js in config folder and change the code from 在config文件夹中打开routes.js并更改代码
'/': {
       view: 'homepage'
    }

to

'/': {
       view: false
    }

The assets folder serves as the root folder in the website. assets文件夹用作网站中的根文件夹。 If your "angular.min.js" file is located in "assets\\bower_components\\angular" folder, this will be converted to "/bower_components/angular/angular.min.js" in the browser. 如果“angular.min.js”文件位于“assets \\ bower_components \\ angular”文件夹中,则会在浏览器中将其转换为“/bower_components/angular/angular.min.js”。

May not be exactly what you are looking for, but there is a project that I looked at awhile ago which seems to do something similar; 可能不是你正在寻找的,但是我前面看过一个似乎做类似事情的项目; combining front-end (jquery/angular) + back-end(sails). 结合前端(jquery / angular)+后端(sails)。

http://sanestack.com/ http://sanestack.com/

The main difference is that the sanestack is using ember as the front end (which may not be for you). 主要区别在于sanestack使用余烬作为前端(可能不适合你)。 But the nice thing about it is the generators create the appropriate resources on the front-end + back-end projects simultaneously. 但关于它的好处是生成器同时在前端+后端项目上创建适当的资源。

At the least, hopefully it will give you some ideas of how to combine sails with a front-end project. 至少,希望它会给你一些如何将风帆与前端项目相结合的想法。

As mentioned in this answer you can easily use Sails to serve your static site. 本回答所述,您可以轻松使用Sails为静态站点提供服务。

However I would also like to recommend an alternate approach - since your application frontend is cleanly decoupled from your frontend you can utilize this decoupling to scale your backend and frontend separately using different strategies. 但是我还想推荐一种替代方法 - 因为你的应用程序前端与你的前端干净地分离,你可以利用这种解耦来使用不同的策略分别扩展你的后端和前端。

So right now, assuming that you deploy your application on a single instance, you can use Nginx that can serve static assets very efficiently and use that as a reverse proxy to your API. 所以现在,假设您在单个实例上部署应用程序,您可以使用可以非常有效地提供静态资产的Nginx,并将其用作API的反向代理

Eventually when you need to scale your application and would like to deploy the application to a cluster of instances (let us say on AWS) you can push your static assets on an S3 bucket and deploy your API on EC2 instances. 最终,当您需要扩展应用程序并希望将应用程序部署到实例集群(让我们在AWS上说)时,您可以将静态资产推送到S3存储桶并在EC2实例上部署API。 You will not have to redeploy your API each time you have to make some visual change on the frontend. 每次必须在前端进行一些视觉更改时,您都不必重新部署API。 You can serve your entire frontend from a CDN (like AWS cloudfront) that grabs assets from a single S3 bucket. 您可以从CDN(如AWS云端)服务整个前端,该CDN从单个S3存储桶中获取资产。 The instances serving your API will be entirely unburdened from the overhead of serving static assets. 为您的API提供服务的实例将完全免除服务静态资产的开销。

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

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