Is it bad practice to build the server API as part of a brunch application?

421 views Asked by At

I am using brunch to build a single-page web application. The application talks to an backend CRUD API that I would like to write using node.js/express. The natural thing to me seems to be to build out the server in a subdirectory of my application, parallel to app. This has the advantage of having all of the code under one roof as well has allowing me to start everything up via brunch watch --server.

I started doing this and then I became worried. If I have server-side dependencies that I install via npm install --save-dev some-server-dependency, do these dependencies get embedded into the javascript of my single-page application? This seems like it would unnecessarily increase the size of my app. If this doesn't happen, how does brunch know what dependencies to include in vendor.js?

This leads to more general questions: Is it bad practice to develop the API in the same project as the client code? If so, are there any brunch equivalents for building the server-side API?

1

There are 1 answers

2
es128 On BEST ANSWER

This is not bad practice, and there are Brunch skeleton examples that do the same thing. You've already mentioned that you're putting your server-side code in a separate directory from app, so as long as that other directory is not in the paths.watched of your brunch config, then your back-end code will not be included in your concatenated front-end code. You can even have shared code between your client and server apps, which is great for things like input validation.

There may be situations where you'd eventually want to break out your server side code into something separate, but the approach you'd described should make that very easy to accomplish if/when that time comes.

To answer that last question regarding brunch equivalents for node applications, there are several tools that will automatically restart a node process whenever you edit its code such as nodemon, supervisor, and node-dev. Since Brunch won't handle this part for you (a future version may add this ability), anytime you are actively hacking on server side code you may want to run brunch watch and the server separately with one of these tools.