Node JS, Express Structure and Require Confusion - javascript

This has been driving me crazy. I'm new to NodeJS. I love it so far but some things have been throwing me off. I was handed a very basic starting point to a node project and I'm unsure how to search google for this.
//myapp/server.js
var config = require('./config');
var app = express();
var api = require('./app/routes/api')(app, express); // <-- this?
app.use('/', api);
var server = app.listen(3000, function () {
console.log('\n============================');
console.log(' Server Running on Port 3000 ');
console.log('============================\n');
});
Then there's the api.js file that contains the routes.
//myapp/app/routes/api.js
var config = require('../../config');
var mysql = require('mysql');
module.exports = function(app, express) {
var api = express.Router();
api.all('/', function(req, res) {...});
api.all('/route-two', function(req, res) {...});
api.all('/another-route', function(req, res) {...});
return api;
}
Ideally I'd like to break up what's going on here into a more organized structure but, I want to understand what I'm doing exactly.
The main thing that is confusing me is this line
var api = require('./app/routes/api')(app, express);
I was unaware that you can have ()() next to each other without a . or something joining them. Can someone explain what is happening?
Also what is the point of (app, express)? It appears that app and express are getting passed to the api part of the application so it's scope can be reached? Am I way off?
If there is a cleaner approach to this I would love to get some insight. I appreciate any thoughts.
Thanks!
EDIT
To make sure I am understanding...
var api = require('require this file')(params available to this file);
Moving any requires from api.js to server.js then include those as parameters
var api = require('./app/routes/api')(config, app, express, mysql);
EDIT
After more helpful feedback from #AhmadAssaf #Gurbakhshish Singh and #guy mograbi
Modules I want to use in another file other than where they are require()ed should be passed in through the second set of ()
//.server.js
var config = require('./config');
var app = express();
var api = require('./app/routes/api')(config, app, express);
| | |
_____________/______/________/
/ / /
//.app/routes/api.js | | |
module.exports = function(config, app, express) {
var api = express.Router();
// code to handle routes
}
Could be wrong with this part but based on what I think I am understanding.
//.server.js
var config = require('./config');
var app = express();
var register = require('./app/routes/register')(config, app, express);
var login = require('./app/routes/login')(config, app, express);
| | |
_________________/______/________/
/ / /
//.app/routes/login.js | | |
module.exports = function(config, app, express) {...handle login...}
//.app/routes/register.js
module.exports = function(config, app, express) {...handle registration...}
etc. etc.
Hopefully my thinking is about right. I appreciate everyones help on this! :)

So basically you have to understand few thing
module.exports wraps a Javascript object and export it to be used as a pluggable piece of code around a node.js application
The wrapped javascript object can be a JSON object, Javascript variable, function, etc.
What you have up there in the api module is a function that takes two parameters. When you require that module you want to pass some constructors to that function and thats the use of the second () after the module name in the first ()
requiring express once in your program and passing the variable around is more or less a singleton pattern. What you can also do is pass the config object as well to the api module instead of requiring it again :)

var api = require('./app/routes/api')(app, express);
is Equivalent to:
var myFunc = require('./app/routes/api');
var api = myFunc(app, express);
and becauase of NodeJS's module loading procedure, the require('...') will be
plugged in by the piece of code that was exported at the path, it can be a object, function, simple variable, etc.
And as far as ()() goes the require() nodeJS will make it something like function(){}() in your case and this is valid javascript and rather very useful to write IIFE(Immediately-Invoked Function Expression) code

Quesiton 1
explain ()()
every language where a function can return a function you can have this syntax. imagine the following
function world(){ ... }
function hello(){
return world;
}
// ===>
hello()() // ==> would invoke hello and then world.
So when you see require('..')() then it means require('..') returns a function. You do this by writing the following:
module.exports = function(){}
and that function returns yet another function - in your case this means express.Router(); returns a function.
Question 2
is there a cleaner way to write this?
this is a discussion.. which is hard to answer. depends on your preferences. The only thing I can think of that might help you reach an answer is to use the express generator and see the structure the express team uses.. which is probably as clean as it gets.
express can generate a project for you with some code to start with. simply install it with npm install -g express and then run express - it will generate the project for you in the same directory where you ran it.
go over the generated project. I suggest follow the same pattern - this is what i do whenever i cick-off a project.
If something is still unclear or if you need me to elaborate, please comment so and I will edit the answer.

module.exports from api.js is a function, which takes two arguments: app, and express. Therefore, when you require it in server.js with require('./app/routes/api'), the value returned is that function. Since it's a function, you can just call it by putting parentheses after it, and passing in the arguments it expects (app and express), like so : require('./app/routes/api')(app, express).

Related

how to access express module for other js file , express module is already included in app.js

Let Me explain what I am working on .
I have included the express module in app.js .
Here I want to access this module in index.js , admin.js , gallery.js.
but I dont want to write the same code again in all over js file .
let me show you my scenario.
app.js has
var express = require('express');
index.js has
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
gallery.js has
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('gallery', { title: 'Gallery' });
});
module.exports = router;
Here var express = require('express'); is already present in app.js .
I want to reuse it from app.js instead of including in every file .
What I have tried so far
I made global in app.js , so that I can access it all over
global.express = require('express');
this code is working for me .
Is this the correct approach or any other way to do or including express module in all the js file is fine ?
You can export your files (not app.js assuming this is your entry point) as a function and pass your express module into those files
gallery.js
module.exports = function(app) {
/* GET users listing. */
app.get('/', function(req, res, next) {
res.render('gallery', { title: 'Gallery' });
});
}
app.js
var express = require('express')
var app = express();
var galleryRoute = require('./gallery');
// Routes
galleryRoute(app);
I have read some where to avoid using Global Variables because there are some cons of it here are few of them:
The first reason is that when you create a global variable, it exists
throughout the lifetime of the application. When a variable persists
through the lifetime of the app it means that it is there, in memory,
occupying resources while the app is running.
Second, traditionally using global variables can cause concurrency
issues. If multiple threads can access the same variable and there
are no access modifiers or failsafes in place, it can lead to some
serious issues of two threads attempting to access and use the same
variable. However, while this is the case in other languages, it is
not necessarily the case for Node.js as it is strictly a
single-threaded environment. While it is possible to cluster Node
processes, there is no native way to communicate between them.
The last reason I am going to talk about is that using globals can
cause implicit coupling between files or variables. Coupling is not a
good thing when it comes to writing great code. When writing code, we
want to make sure that it is as modular and reusable as possible,
while also making sure it is easy to use and understand. Coupling
pieces of your code together can lead to some major headaches down
the road when you are trying to debug why something isn't working.
As for your question you can export the express from app.js or index and use it everywhere.
It is discouraged to use additional global variables as application already has 'export' and 'require' global variables. (exports/require are not keywords, but global variables.)
If you need to export the 'app' for other files, you can do something like below.
in index.js:
var express_app = module.exports = express();
now index.js can be required to bring app into any file.
in any.js file:
var express_app = require('./index');

How to separate the code into separate files? [duplicate]

I'm developing my first Node.js App with Socket.IO and everything is fine but now the app is slowly getting bigger and I'd like to divide the app-code into different files for better maintenance.
For example I'm defining all my mongoose schemas and the routings in the main file. Underneath are all the functions for the socket.IO connection. But now I want to have an extra file for the schemas, an extra file for routing and one for the functions.
Of course, I'm aware of the possibility to write my own module or load a file with require. That just does not make sense for me, because I can't work with the vars like app, io or db without making them global. And if I pass them to a function in my module, I can't change them. What am I missing? I'd like to see an example how this is done in practice without using global vars..
It sounds like you have a highly coupled application; it's difficult for you to split out your code into modules because pieces of the application that should not depend on each other do. Looking into the principles of OO design may help out here.
For example, if you were to split your dataabse logic out of the main application, you should be able to do so, as the database logic should not depend on app or io--it should be able to work on its own, and you require it into other pieces of your application to use it.
Here's a fairly basic example--it's more pseudocode than actual code, as the point is to demonstrate modularity by example, not to write a working application. It's also only one of many, many ways you may decide to structure your application.
// =============================
// db.js
var mongoose = require('mongoose');
mongoose.connect(/* ... */);
module.exports = {
User: require('./models/user');
OtherModel: require('./models/other_model');
};
// =============================
// models/user.js (similar for models/other_model.js)
var mongoose = require('mongoose');
var User = new mongoose.Schema({ /* ... */ });
module.exports = mongoose.model('User', User);
// =============================
// routes.js
var db = require('./db');
var User = db.User;
var OtherModel = db.OtherModel;
// This module exports a function, which we call call with
// our Express application and Socket.IO server as arguments
// so that we can access them if we need them.
module.exports = function(app, io) {
app.get('/', function(req, res) {
// home page logic ...
});
app.post('/users/:id', function(req, res) {
User.create(/* ... */);
});
};
// =============================
// realtime.js
var db = require('./db');
var OtherModel = db.OtherModel;
module.exports = function(io) {
io.sockets.on('connection', function(socket) {
socket.on('someEvent', function() {
OtherModel.find(/* ... */);
});
});
};
// =============================
// application.js
var express = require('express');
var sio = require('socket.io');
var routes = require('./routes');
var realtime = require('./realtime');
var app = express();
var server = http.createServer(app);
var io = sio.listen(server);
// all your app.use() and app.configure() here...
// Load in the routes by calling the function we
// exported in routes.js
routes(app, io);
// Similarly with our realtime module.
realtime(io);
server.listen(8080);
This was all written off the top of my head with minimal checking of the documentation for various APIs, but I hope it plants the seeds of how you might go about extracting modules from your application.

Require (the same instance of) socket.io in multiple modules

I am a bit confused, about how to require and use modules in Node.js.
My scenario is the following:
I wrote a complete server in one single file, which uses Socket.io for realtime communication.
Now the index.js became pretty big, and I want to split the code into several modules to make it more managable.
For example I have some functions for serving a Survey to the clients, and getting back their answers. I put all those functions in a seperate module, and require it in the index.js. Works fine so far.
The only thing I am concerned about is, if there is another way to use the SAME socket instance inside the module.
My current coding looks like this:
index.js:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var Survey = require('./survey');
io.on('connection', function (client) {
client.on('getCurrentQuestion', function (data) {
Survey.getCurrentQuestion(parseInt(data.survey_id), client.id);
});
});
server.listen(port, server_url, function () {
Survey.init(io);
});
survey.js:
var io = null;
var Survey = {};
Survey.init = function(socketio) {
io = socketio;
};
Survey.getCurrentQuestion = function(survey_id, socket_id) {
var response = {
status: "unknown",
survey_id: survey_id
};
// [...] some code that processes everything
// then uses Socket.io to push something back to the client
io.sockets.in(socket_id).emit('getCurrentQuestion', response);
};
module.exports = Survey;
This way it works, but I'm not happy passing io inside an init function to the required module.
What would be "the right way" to do this?
If I require('socket.io') inside the survey module, will it be the same instance as in index.js?
How would I even require that, since it needs the server, which needs the app, which is created in index.js?
I am confused and hope that somebody can help me. Thanks!
When you import a node.JS library you can also pass in objects. In your case the index.js file should be changed to the following:
//index.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var Survey = require('./survey')(io);
Then just change your survey.js code to take the io object:
//survey.js
module.exports = function (io) {
var Survey = {};
Survey.getCurrentQuestion = function(survey_id, socket_id) {
var response = {
status: "unknown",
survey_id: survey_id
};
// [...] some code that processes everything
// then uses Socket.io to push something back to the client
io.sockets.in(socket_id).emit('getCurrentQuestion', response);
};
return Survey;
};
To answer your other question:
If you require('socket.io') inside the survey module, it will be a different instance from index.js.
EDIT
If you want a more modern way of doing it...You could use ES6 format and create a class to do this better:
'ES6 index.js
import SurveyClass from './Survey';
import * as express from 'express';
let app = express();
let server = require('http').createServer(app);
let io = require('socket.io')(server);
let MySurveyClass= SurveyClass(io);
let myInstance = new MySurveyClass();
myInstance.getCurrentQuestion(5, "some-socket-id");
'ES6 survey.js
export default class Survey{
constructor(io){
this.io= io;
};
getCurrentQuestion(survey_id, socket_id) {
var response = {
status: "unknown",
survey_id: survey_id
};
// [...] some code that processes everything
// then uses Socket.io to push something back to the client
this.io.sockets.in(socket_id).emit('getCurrentQuestion', response);
};
}
When you do this in multiple modules:
var socketio = require('socket.io');
then it will be the same object for all requires.
But if you do this:
var io = require('socket.io')(server);
even if you have the same object in server in both places, io would be a different value because it would come from different invocations of the function returned by require('socket.io') even though those functions would be the same.
If you want to make sure that the io is the same then you'd have do e.g. something like this: Make a module that exports a promise of io and some way to initialize it - a function that gets the server and everything needed. Now, in the place where you have the server you can require the module and initialize it with that exported function, and in other places you can import the module and use a promise.
Instead of a promise you could use callbacks or even just a property on the exported object that could be initially undefined and gets defined when the socket.io is initialized but using a promise would probably be most simple solution to make sure that you're not using it while it's not ready yet.
You didn't mention in your question whether or not you use some framework and which one if any, but some of the frameworks like Hapi give you an easy way to share functionality like that. E.g. see:
https://hapijs.com/tutorials/plugins
If you use Hapi then you should use plugins. If other framework then search for a similar feature.

nodejs how to execute other js file from index.js?

I want to create web app with nodejs but with structure like this
- modules
| - module1
| - module1.js
| - module2
| - module2.js
- index.js
- settings.js
First, i never use nodejs from scratch
Second, i know you will suggest using any other framework, sorry guys here i want to understand how nodejs and may be other nodejs framework works.
Third, this is for educational purpose
in modules/module1/module1.js :
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('Hello World aw!')
honestly i dont know how to code it properly, what i really want is i wanna execute that code and response to browser Hello World aw! from index.js :
router.get('/', function (req, res) {
//in here i call modules/module1/module1.js, and execute the code
})
and how to do that properly? so it can run well
to load any script or module in nodejs you need to use require
like this
var module1 = require(__dirname + 'modules/module1/module1.js');
and then you can use module1 anywhere in your code
BTW for express you should create routes and then attach that routes to your app. See example below.
users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
server.js
var express = require('express');
var app = express();
var users = require('./routes/users');
....
app.use('/users', users);
Every time with node when you need to have a module in a particular page you need to require this module.
example: you want to get the library express so at the top of your file who you want this library you do something like that.
var express = require('express');
Now you can use express in all the file. When this is a node library you just need to require the name.
this is the same thing with a file you create and you want inside a other one
var myfile = require('./../myfile');
with that you can use the word myfile inside all the file. But you need to figure out the path to this one.
A good links for read about this

How to structure a express.js application?

Is there a common convention for breaking up and modularizing the app.js file in an Express.js application? Or is it common to keep everything in a single file?
I have mine broken up as follows:
~/app
|~controllers
| |-monkey.js
| |-zoo.js
|~models
| |-monkey.js
| |-zoo.js
|~views
| |~zoos
| |-new.jade
| |-_form.jade
|~test
| |~controllers
| |-zoo.js
| |~models
| |-zoo.js
|-index.js
I use Exports to return what's relevant. For instance, in the models I do:
module.exports = mongoose.model('PhoneNumber', PhoneNumberSchema);
and then if I need to create a phone number, it's as simple as:
var PhoneNumber = require('../models/phoneNumber');
var phoneNumber = new PhoneNumber();
if I need to use the schema, then PhoneNumber.schema
(which assumes that we are working from the routes folder and need to go 1 level up and then down to models)
EDIT 4
The express wiki has a list of frameworks built on top of it.
Of those, I think Twitter's matador is structured pretty well. We actually used a very similar approach to how they load up parts of the app.
derby.js also looks extremely interesting. It's akin to meteor without all of the hype and actually gives credit where credit is due (notably, node and express).
EDIT 3
If you are a fan of CoffeeScript (I am not) and reeeeaaaaaally want the L&F of Rails, there is also Tower.js.
EDIT 2
If you are familiar with Rails and don't mind the bleed-over of some concepts there is Locomotive. It is a light-weight framework built on Express. It has a very similar structure as RoR and carries over some of the more rudimentary concepts (such as routing).
It's worth checking out even if you don't plan to use it.
EDIT 1
nodejs-express-mongoose-demo is very similar to how I have mine structured. Check it out.
Warning: referencing code I hacked together for node knockout, it kind of works but is far from elegant or polished.
To be more specific about splitting up app.js I have the following app.js file
var express = require('express'),
bootstrap = require('./init/bootstrap.js'),
app = module.exports = express.createServer();
bootstrap(app);
This basically means I place all my bootstrapping in a seperate file, then I bootstrap the server.
So what does bootstrap do?
var configure = require("./app-configure.js"),
less = require("./watch-less.js"),
everyauth = require("./config-everyauth.js"),
routes = require("./start-routes.js"),
tools = require("buffertools"),
nko = require("nko"),
sessionStore = new (require("express").session.MemoryStore)()
module.exports = function(app) {
everyauth(app);
configure(app, sessionStore);
less();
routes(app, sessionStore);
nko('/9Ehs3Dwu0bSByCS');
app.listen(process.env.PORT);
console.log("server listening on port xxxx");
};
Well it splits all the server initialization setup in nice chunks. Specifically
I have a chunk that sets up all my remote OAuth authentication using everyauth.
I have a chunk that configures my application (basically calling app.configure)
I have a little bit of code that punches less so it re-compiles any of my less into css at run time.
I have code that sets up all my routes
I call this small nko module
Finally I start the server by listening to a port.
Just for example let's look at the routing file
var fs = require("fs"),
parseCookie = require('connect').utils.parseCookie;
module.exports = function(app, sessionStore) {
var modelUrl = __dirname + "/../model/",
models = fs.readdirSync(modelUrl),
routeUrl = __dirname + "/../route/"
routes = fs.readdirSync(routeUrl);
Here I load all my models and routes as arrays of files.
Disclaimer: readdirSync is only ok when called before you start the http server (before .listen). Calling synchronious blocking calls at server start time just makes the code more readable (it's basically a hack)
var io = require("socket.io").listen(app);
io.set("authorization", function(data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionId = data.cookie['express.sid'];
sessionStore.get(data.sessionId, function(err, session) {
if (err) {
return accept(err.message, false);
} else if (!(session && session.auth)) {
return accept("not authorized", false)
}
data.session = session;
accept(null, true);
});
} else {
return accept('No cookie', false);
}
});
Here I punch socket.io to actually use authorization rather then letting any tom and jack to talk to my socket.io server
routes.forEach(function(file) {
var route = require(routeUrl + file),
model = require(modelUrl + file);
route(app, model, io);
});
};
Here I start my routes by passing the relevant model into each route object returned from the route file.
Basically the jist is you organize everything into nice little modules and then have some bootstrapping mechanism.
My other project (my blog) has an init file with a similar structure.
Disclaimer: the blog is broken and doesn't build, I'm working on it.
For maintainable routing organisation you can check out this article about the express-routescan node module and try it. This is the best solution for me.
I have my apps build on top of the express-generator tool. You can install it by running npm install express-generator -g and run it using express <APP_NAME>.
To give you a perspective, one of my smaller application's structure looked like this:
~/
|~bin
| |-www
|
|~config
| |-config.json
|
|~database
| |-database.js
|
|~middlewares
| |-authentication.js
| |-logger.js
|
|~models
| |-Bank.js
| |-User.js
|
|~routes
| |-index.js
| |-banks.js
| |-users.js
|
|~utilities
| |-fiat-converersion.js
|
|-app.js
|-package.json
|-package-lock.json
One cool thing I like about this structure I end up adopting for any express application I develop is the way the routes are organized. I did not like having to require each route files into the app.js and app.use() each route, especially as the file gets bigger. As such, I found it helpful to group and centralize all my app.use() on a ./routes/index.js file.
In the end, my app.js will look something like this:
...
const express = require('express');
const app = express();
...
require('./routes/index')(app);
and my ./routes/index.js will look something like this:
module.exports = (app) => {
app.use('/users', require('./users'));
app.use('/banks', require('./banks'));
};
I am able to simply require(./users) because I wrote the users route using express.Router() which allows me to "group" multiple routes and then export them at once, with the goal of making the application more modular.
This is an example of what you would fine on my ./routers/users.js route:
const router = require('express').Router();
router.post('/signup', async (req, res) => {
// Signup code here
});
module.exports = router;
Hopefully this helped answer your question! Best of luck!

Categories