Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a custom Express server with Ember CLI?

Tags:

ember-cli

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. However I would like to program a custom Express server and have my app point to that Node.js code for its backend. How would I go about doing that within the Ember CLI framework?

UPDATE:

Following @user3155277's answer, I added an adapter file like so:

app-name/app/adapters/application.js:

import DS from 'ember-data';

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

I created an Express server that I put at the root of my app:

app-name/server.js:

var express = require("express"),
    app = express(),
    path = require("path");

app.get("/api/test", function(req, res) {
    res.json({
        hello: "world"
    });
});

var server = app.listen(8147);

In the Ember app, my index route is defined as so:

app-name/app/routes/index.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return Ember.$.getJSON("/api/test").then(function(data) {
            return data;
        });
    }
});

On the command line I then start the server like so:

ember serve --proxy http://localhost:8147/

I get the following error:

version: 0.0.35-master-86abdb11ba
Proxying to http://localhost:8147/
object is not a functionTypeError: object is not a function
    at Class.module.exports.Task.extend.start (D:\ember-cli\lib\tasks\server\express-server.js:41:43)
    at Class.module.exports.Task.extend.run (D:\ember-cli\lib\tasks\serve.js:40:23)
    at Class.module.exports.Command.extend.run (D:\ember-cli\lib\commands\serve.js:35:18)
    at Class.Command.validateAndRun (D:\ember-cli\lib\models\command.js:74:15)
    at CLI.<anonymous> (D:\ember-cli\lib\cli\cli.js:33:20)
    at tryCatch (D:\ember-cli\node_modules\rsvp\dist\commonjs\rsvp\-internal.js:163:16)
    at invokeCallback (D:\ember-cli\node_modules\rsvp\dist\commonjs\rsvp\-internal.js:172:17)
    at publish (D:\ember-cli\node_modules\rsvp\dist\commonjs\rsvp\-internal.js:150:13)
    at flush (D:\ember-cli\node_modules\rsvp\dist\commonjs\rsvp\asap.js:51:9)
    at process._tickCallback (node.js:419:13)Livereload server on port 35729
like image 782
cwarny Avatar asked Jun 20 '14 21:06

cwarny


People also ask

Is Ember JS frontend or backend?

It should also be mentioned that Ember is purely a frontend framework. It has a number of ways of interacting with the backend of your choice, but this backend is not in any way handled by Ember itself.

How do I make an Ember project?

To create a new project using Ember CLI, use the new command. In preparation for the tutorial in the next section, you can make an app called super-rentals . A new project will be created inside your current directory. You can now go to your super-rentals project directory and start working on it.

What is Ember CLI?

Ember CLI, Ember's command line interface, provides a standard project structure, a set of development tools, and an addon system. This allows Ember developers to focus on building apps rather than building the support structures that make them run.


1 Answers

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.

like image 155
cwarny Avatar answered Oct 22 '22 02:10

cwarny