How to use a custom Express server with Ember CLI?

后端 未结 2 1295
予麋鹿
予麋鹿 2020-12-24 09:51

I\'m using Ember CLI 0.0.36. When I run ember server in my project folder, my understanding is that a server buried in some Brocoli process gets started. Howeve

相关标签:
2条回答
  • 2020-12-24 09:56

    I started playing with ember cli so i'm not sure but i found the following: https://github.com/dockyard/ember-cli-plus-backend/tree/rails-served-html/frontend/api-stub Basically you should proxy your express server to the same port as your ember-cli (so then you don't have to deal with jsonp issue)

    Set the method to 'proxy' and define the proxyURL to pass all API requests to the proxy URL.

    UPDATE:

    1.Generate adapter ember generate adapter application

    2.Make api namespace - fill the created file with the following code:

    export default DS.RESTAdapter.reopen({ namespace: 'api' });

    3.Start the server with ember serve --proxy http://localhost:1337/ (this will proxy all your request to localhost:4200 to 1337

    4.Make routes in your express app prefixed by /api

    Hope it helps

    0 讨论(0)
  • 2020-12-24 10:02

    This is actually pretty simple with Ember CLI 0.0.40:

    Create folder structure

    ember new my-app
    

    Go into the newly created folder

    cd my-app
    

    Generate api-stub* (see update)

    ember generate api-stub my-server
    

    This latter command creates a server folder with an index.js file and a routes folder with a my-server.js file.

    Open my-server.js file and you see:

    module.exports = function(app) {
        var express = require("express");
        var myServerRouter = express.Router();
        myServerRouter.get("/", function(req, res) {
            res.send({my-server:"something"});
        });
        app.use("/api", myServerRouter);
    };
    

    All you need to do then is to change that file. If the Ember app makes calls to /api/hamsters and /api/project, edit as follows:

    module.exports = function(app) {
        var express = require("express");
        var myServerRouter = express.Router();
        myServerRouter.get("/hamsters", function(req, res) {
            res.send({ ... });
        });
        myServerRouter.get("/project", function(req, res) {
            res.send({ ... });
        });
        app.use("/api", myServerRouter);
    };
    

    To start the server (from project's root):

    ember server
    

    Make sure you have updated node.js to the latest version as well.


    Updates

    As of Ember CLI 0.0.41 (via this PR) api-stub has been renamed http-mock.

    0 讨论(0)
提交回复
热议问题