I'm just curious how people structures their Node.js app?
Usually I create models/ views/ controllers/ and that's simple as that. But I'm kinda new to the Node.js scene and I'm trying to learn as much as I can about how the community works.
Any answer is welcome, thanks!
For what it's worth, my actual setup is this, until I come up (or find) something clearly better:
lib
db
index.js
model.js
...
handler
index.js
whateverMakesSenseForMyParticularWebSite.js
...
router
index.js
model1RestRoutes.js
model2RestRoutes.js
iuRoutes.js
...
config.js (or a folder with multiple files if it makes sense)
server.js (main)
public
css
img
js
test
...
views
...
So yes, models, views, but I do separate routes and actual handlers' implementation. Decoupling, dependency injection all the way. Way more testable/mockable.
Related
I'm trying to use intl-tel-input in a project I'm building using express and ejs.
I have app.use(express.static(path.join(__dirname, 'public'))); in my app.js which means Express serves all static files from the public directory in my project.
I am unable to integrate intl-tel-input as it's a node module and the relevant css and js files are located in /node_modules/intl-tel-input/build/... and they are not served by Express at all. Thus, when I try to link to them in my Ejs template, I get a 404 error.
How do I solve this? All the ideas I have seem like the wrong approach.
After struggling with this for a while and exploring solutions like Webpack, RequireJS and other not so elegant ones, I found this answer to be the most relevant, easy and effective solution for this particular problem.
Node.js is special because the backend and frontend use the same language. I wanted to take advantage of this and only write 1 set of util functions (universal functions like numToPercent) for backend and frontend. Here is how my (important) files are structured right now:
project
public
index.html
index.js
scripts
script1.js
index.js
otherScript.js
Where is the canonical location to put my util file?
Edit: to make this more specific, is there a "shared" folder or something to put this file? Because otherwise public seems like the best place to put this
You can package the shared code and add it as a dependency via npm.
I'm making a scalable REST-API, but I barely find articles on advanced node.js application structures in terms of a big application, since most of them are using simple starter projects.
I'm using the "folder-by-feature" structure, based on this article and this answer.
My question: what is the better structure of the solutions bellow?
1. Keep the routes in a separate folder:
src
product
index.js
product.spec.js
routes
index.js
product.js
user.js
user
index.js
user.spec.js
2. Put the route into its corresponding folder:
src
product
index.js
product.route.js
product.spec.js
user
index.js
user.route.js
user.spec.js
Using the routes in the index.js files.
Are there any better solutions?
Any article about advanced, scalable node project structures would be appreciated!
Since this is an opinion question here is mine:
My build migrates everything from src to dist. Some is compiled and some is just copied. I then run directly from the dist folder.
src
api
<api files/folders>
lib
<common lib files/folders>
routes
<Route files (app.use, app.get, etc.)>
static
<static css, images, script, etc.>
<I do not include src code that is compiled in any way>
ui
<LESS, SASS, JS, etc that will be compiled, combined, packed, etc>
views
<ejs files>
app.js
Things in src/ui/** get compiled and placed into dist/static/**.
I am using express.js for my website. I have created the directory structure using 'express-generator'. It contains 'views' folder to render views. It also has 'routes' folder for routing purpose. Now my question is where should I define or do the core logic code?
For organization purposes, I use /controllers for my, well, controllers (which I believe is what you mean). Express really doesn't care where you put your code, just as long as you correctly use require('./path/to/whatever/directoryname/you/choose/filename') in your routes and views and module.exports = controllerToExpose; in your controllers.
Is there a way to use Chaplin with node.js/express? I haven't found a single tutorial or an example on this subject.
If so, how do I get started? How would the folder structure look like? Or my server.js file?
Chaplin appears to be purely client side. For a basic app your back end could be a static HTML page. It requires no particular server structure.
Backbone, which it's built on, expects a RESTful JSON API to persist its models, but otherwise doesn't require a backend either. If your app has models which need to be saved (likely), then you'll want to look into tutorials for writing a REST API in express (there are many) or for extending Backbone to suit your particular backend needs.
So to get started, your server.js file will look exactly like the one created by the express install script.
There's no de facto best practice for how to structure the folders in an end-to-end javascript app. In my experience I tend to keep client side javascript in its own folder (/client, /app/client, /lib/client, etc), then generate the publicly exposed compressed/concated scripts in a build step. To get started, you might just deposit them in /public/javascripts.
I still don't understand this..
In my express file it says
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
Should I move all my chaplin app files into that views folders or how do I launch them?
Anyway, express seems to use this jade thing but chaplin does not, how does that go?
My problem is to understand where everything goes and why.
Express creates it's own folder structure:
node_modules
public
routes
views
app.js
then again chaplin has it like this:
app
generators
bower.json
config.json
etc.
in that app folder:
assets
controllers
lib
models
views
Now, where do I place all of this chaplin stuff in my node folders? under public or views or where?
And then how do I get this whole thing started? Do I just include all the chaplin .js files in that index file I have in my node.js views folder (jade file)?