Am I Using Express with Ember "right"?

Hello Ember community. :slightly_smiling:

I am new to Ember after deciding Angular 2 is too freaking weird and unattractive.

I am developing a full-stack app using Ember and Express.

My file structure

(This is inside the main project folder)

  • client (made from ember-cli generator)
  • server (server routes, etc.)
  • api (api routes)
  • server.js (use command node server.js to start)

Plus some additional top-level server stuff.

How am I working with my own REST API and serving Ember files?

Server.js

//server.js

...

////////////////////////////////////////////////////////
// Init routes                                        //
// APIs *MUST* be declared first, as everything after //
// will render Ember client code.                     //
////////////////////////////////////////////////////////

// Allows call to my REST API
require('./api/core')(server);

// Now set server to look at generated ember client files
server.use(express.static(__dirname + '/client/dist'));
server.use('/', require('./server/routes'));

...

/server/routes.js

//routes.js

...

// Location to built dist by Ember
var rootPath = './client/dist/';

// Route ALL locations to index.html, the main Ember app
router.get('*', function(request, response) {

    // Send index.html
    response.sendFile('index.html', { root: rootPath });
});

module.exports = router;

...

I think the key part is that everything works. I tested my API, which returns successfully. I also tested going to / (index) and /about (sample about page to test a non-index route). Both work as intended. :slight_smile:

tl;dr So the question is: is this the proper way to be doing things with Ember and Express?

From my point of view, it appears that I have the “middle” set up and can begin developing the front-end (Ember) and backend (REST API) separately, as intended.

I think the right way would be use one of these libraries

http://jsonapi.org/implementations/#server-libraries-node-js

Could you please elaborate?

The standard Ember package comes with Ember Data, the modeling layer.

The default adapter used is JSONAPIAdapter which implements the json-api spec, and Ember actively encourages people to adopt json-api as their API interface.

The link above describes a list of json-api server implementations. I can’t really say which one is the best. You’ll have to decide for yourself.

A catchall route will work fine @mathshane. Just use expressjs’ compression lib to get gzip enabled.

https://github.com/expressjs/compression

And one other suggestion is mounting the api using a separate route on /api to keep it complete separate and allow for easy versioning of your endpoints.

@lightblade

Ah, very neat. Thank you. I was specifically referring to my general server setup, but I will definitely look into those libraries for handling the JSON.

@jasonmit

Awesome!

Just use expressjs’ compression lib to get gzip enabled.

If I got this right, I am compressing the client “stuff” and letting the client uncompress it.

Presumably I should leave the REST API JSON as is (sending raw JSON)?

And one other suggestion is mounting the api using a separate route on /api to keep it complete separate and allow for easy versioning of your endpoints.

Yep. I just wanted to setup the general path before I work on the specifics. I will probably do something like /api/v1 and then branch off (e.g. /api/v1/core and /api/v1/user, etc.).

Correct, all modern browsers support gzip.

Correct as well

Just out of curiosity, what port are you running the server on? By default ember is running on port 4200, I usually run express 8000 so I’m not sure how that should work. Sorry, really not trying to hijack the thread, this just happens to be the most relevant question on this issue.