Web Dev Questions That Don't Need Their Own Thread v4
5,001 replies, posted
[QUOTE=Coment;47666368]$('.icon-menu').click(function());
It's waiting for the function to have code to run between {}, but it gets the ); instead, so it doesn't know what to do.[/QUOTE]
ah i see
Was bored and decided to start a Node project and got myself express along the way. I wanted to move my routes outside my index.js to organise it a bit better and I found out that doing this will work:
[B]index.js[/B]
[code]
var express = require('express');
var app = express();
require('./app/routes.js')(app);
app.listen(3000);
[/code]
[B]app/routes.js[/B]
[code]
module.exports = function(app) {
app.get('/', function() {
return 'Hello World';
});
}
[/code]
Problem is that IMO it looks really ugly to have your routes nested in a closure like that, is there a way to get around that somehow? Or is this not how you would make a proper express application? I've seen people throw individual routes into their own file, but that just seems like a very bad way to do it. :v:
[QUOTE=Moofy;47676362]Was bored and decided to start a Node project and got myself express along the way. I wanted to move my routes outside my index.js to organise it a bit better and I found out that doing this will work:
[B]index.js[/B]
[code]
var express = require('express');
var app = express();
require('./app/routes.js')(app);
app.listen(3000);
[/code]
[B]app/routes.js[/B]
[code]
module.exports = function(app) {
app.get('/', function() {
return 'Hello World';
});
}
[/code]
Problem is that IMO it looks really ugly to have your routes nested in a closure like that, is there a way to get around that somehow? Or is this not how you would make a proper express application? I've seen people throw individual routes into their own file, but that just seems like a very bad way to do it. :v:[/QUOTE]
Use this:
[code]var express = require("express"),
router = express.Router();
router.route("/")
.get(function(req, res) {
res.send("Hello world!");
})
.post( /*same shit*/ );
module.exports = router;
[/code]
Then in your app use:
[code]app.use(require("/path/to/that/file.js"));[/code]
[QUOTE=Moofy;47676362]Was bored and decided to start a Node project and got myself express along the way. I wanted to move my routes outside my index.js to organise it a bit better and I found out that doing this will work:
[B]index.js[/B]
[code]
var express = require('express');
var app = express();
require('./app/routes.js')(app);
app.listen(3000);
[/code]
[B]app/routes.js[/B]
[code]
module.exports = function(app) {
app.get('/', function() {
return 'Hello World';
});
}
[/code]
Problem is that IMO it looks really ugly to have your routes nested in a closure like that, is there a way to get around that somehow? Or is this not how you would make a proper express application? I've seen people throw individual routes into their own file, but that just seems like a very bad way to do it. :v:[/QUOTE]
Standard setup I go with:
[B]server/routes.js:[/B]
[code]
module.exports = function(app) {
// Insert routes below
app.use('/api/thing', require('./api/thing'));
app.use('/auth', require('./auth'));
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
};
[/code]
[B]server/api/thing/index.js:[/B]
[code]
var express = require('express');
var controller = require('./thing.controller');
var auth = require('../../auth/auth.service');
var router = express.Router();
router.get('/', controller.index);
router.post('/', auth.hasRole('admin'), controller.create);
module.exports = router;
[/code]
[B]server/api/thing/thing.controller.js:[/B]
[code]
var Thing = require('./thing.model');
exports.index = function (req, res, next) {
// Stuff
};
exports.create = function (req, res, next) {
// Stuff
};
[/code]
[B]And in the main index.js / app.js:[/B]
[code]
require('./config/express')(app);
require('./routes')(app);
[/code]
Whole directory setup:
[CODE]server
api
thing
index.js
thing.controller.js
thing.model.js
thing.socket.js
config
express.js
index.js
routes.js[/CODE]
Wait so you would inside the routes file require express again? What kind of sorcery is this, shouldn't you only require express once in your app.js or whatever?
[QUOTE=Moofy;47679173]Wait so you would inside the routes file require express again? What kind of sorcery is this, shouldn't you only require express once in your app.js or whatever?[/QUOTE]
No, express itself is the module, running express() is setting it up, so including express.Router is just a member of express, not a new copy of express.
[QUOTE=Cyberuben;47679235]No, express itself is the module, running express() is setting it up, so including express.Router is just a member of express, not a new copy of express.[/QUOTE]
Oh okay, thank you!
That's the way modules works in Node. If you export an object you can modify it and the modifications is saved until you restart the process.
For example if I have a module which looks like this:
[code]
var mod = {
things: [],
add: function (data) {
this.things.push(data);
},
remove: function (data) {
this.things.splice(this.things.indexOf(data), 0);
}
};
module.exports = mod;
[/code]
And in some file you modify it:
[code]
var mod = require('mod');
mod.add('Stuff');
console.log(mod.things);
/*
Output:
[ 'Stuff' ]
*/
[/code]
And afterwards in another file:
[code]
var mod = require('mod');
console.log(mod.things);
/*
Output:
[ 'Stuff' ]
*/
[/code]
So the object's state is saved even though you require it multiple times in different files at different points in time, all the way until you kill or restart the process.
In this case, the required 'express' module is simply altered/modified at different times in my script, and is saved in that state. This allows me to keep adding things to it and alter the webserver in real-time, directly in my code as time goes on. Pretty neat if you ask me!
Modules that want to avoid this feature (start over fresh each time) usually constructs a class, e.g:
[code]
function Money (amount) {
this.amount = amount || 0;
}
Money.prototype.double = function () {
this.amount *= 2;
}
Money.prototype.total = function () {
return this.amount;
}
module.exports = Money;
[/code]
Which in two different times "start fresh" so to speak:
[code]
var Money = require('money');
var monies = new Money(2);
monies.double();
console.log(monies.total()); // Output: 4
[/code]
[code]
var Money = require('money');
var monies = new Money(2);
console.log(monies.total()); // Output: 2
[/code]
But I'm guessing you already know how classes work, but this is a neat feature of Node if you, for example want to structure an object of all Socket.io connections but handle them differently in different controllers for example, since the object stays the same (since last updated).
Makes sense, haven't really worked much with Node because in class we're mostly doing PHP and only sometimes we look into things like Rails. So I mostly structure things like how Rails/Laravel does it. :v:
[QUOTE=Moofy;47679859]Makes sense, haven't really worked much with Node because in class we're mostly doing PHP and only sometimes we look into things like Rails. So I mostly structure things like how Rails/Laravel does it. :v:[/QUOTE]
I moved away from PHP and now do Node for pretty much any project. Find it much easier, love npm for it's ease, and I like the workflow better.
PHP's getting old in my eyes.
[QUOTE=Cyberuben;47680287]I moved away from PHP and now do Node for pretty much any project. Find it much easier, love npm for it's ease, and I like the workflow better.
PHP's getting old in my eyes.[/QUOTE]
Personally don't see anything wrong in PHP but obviously there's better choices depending for what you need to make and of course personal opinions.
Either way, I'd just like to get that whole MVC thing going and then I'd like to get something done in Node :v:
Speaking of, I'm getting net::ERR_INCOMPLETE_CHUNKED_ENCODING in a PHP project that I'm helping with.
Unfortunately, it's only affecting a few members of the team, and only affecting chrome (which I hear is stricter about the chunks being properly separated and stuff).
:(
[QUOTE=Rocket;47681007]If you don't see anything wrong with PHP, you haven't spent much time using other languages.[/QUOTE]
Are you [B]really [/B]gonna start this discussion again?
[editline]8th May 2015[/editline]
Last time someone started this shit we had a 3 page argument going on until someone posted that fucking article with bad design or whatever and I'm pretty sure it ended up in most people agreeing that you can't compare languages just like that and things are not bad there's just better options / something along those lines.
[QUOTE=Rocket;47683025]Go try some other languages and environments and then tell me there's nothing wrong with PHP. Like a serious "There is nothing wrong with this language. I wouldn't change a thing about it. It is just fine the way it is."
[editline]8th May 2015[/editline]
There are things wrong with every language I've used. Why is PHP the exception?
[editline]8th May 2015[/editline]
It'd be like me looking at Javascript and saying "Yep, I see no problems with these equality operators. They make sense." I love Javascript, but that's an incredibly dumb and annoying part of the language.
[editline]8th May 2015[/editline]
I don't mean that you have to hate PHP but to say that there isn't anything wrong with it is a huge understatement.[/QUOTE]
He did say earlier he was going to try out node, so calm yourself and stop judging languages. People will use whatever they want to reglardless of your opinion.
[QUOTE=Rocket;47686469]I'm not saying he shouldn't use PHP. But he said there's nothing wrong with PHP and that's a very limiting view.[/QUOTE]
Oh for fucks sake just shut up already, you're taking things [I]waaaay[/I] too literal.
[editline]8th May 2015[/editline]
On topic:
Node is great, pretty simple to get an app going without much trouble. :v:
to put this simply and squash this ridiculous argument, every language has their issues i believe mainly because they are for specific things in general but it's not to say you can use it out of the box for other things like c++ with networking c++ in itself is not really meant for networking purposes but can be used as such but you may have issues with getting it to work properly.
Anyone tried Golang yet? Seems very promising and just keeps on getting more and more popular. I really enjoy the syntax as well! Anyone found any caveats with it yet?
[QUOTE=confinedUser;47688065]to put this simply and squash this ridiculous argument, every language has their issues i believe mainly because they are for specific things in general but it's not to say you can use it out of the box for other things like c++ with networking c++ in itself is not really meant for networking purposes but can be used as such but you may have issues with getting it to work properly.[/QUOTE]
Every language has their issues, but [URL="http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/"]PHP has[/URL] [URL="https://www.reddit.com/r/lolphp"]more[/URL] [URL="https://bugs.php.net/search.php?cmd=display&order_by=id&direction=DESC&limit=30&status=Open&reorder_by=ts1"]issues[/URL] than any other language I know of. You should use the best tool for the job, and PHP hasn't been that tool in nearly 10 years.
[QUOTE=confinedUser;47688065]to put this simply and squash this ridiculous argument, every language has their issues i believe mainly because they are for specific things in general but it's not to say you can use it out of the box for other things like c++ with networking c++ in itself is not really meant for networking purposes but can be used as such but you may have issues with getting it to work properly.[/QUOTE]
just because the c++ standard library doesn't include networking (yet), the whole language is unsuitable for networking? what?
[QUOTE=Simspelaaja;47689698]Every language has their issues, but [URL="http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/"]PHP has[/URL] [URL="https://www.reddit.com/r/lolphp"]more[/URL] [URL="https://bugs.php.net/search.php?cmd=display&order_by=id&direction=DESC&limit=30&status=Open&reorder_by=ts1"]issues[/URL] than any other language I know of. You should use the best tool for the job, and PHP hasn't been that tool in nearly 10 years.[/QUOTE]
[QUOTE=Moofy;47682999]Are you [B]really [/B]gonna start this discussion again?
[editline]8th May 2015[/editline]
Last time someone started this shit we had a 3 page argument going on until someone posted that fucking article with bad design or whatever and I'm pretty sure it ended up in most people agreeing that you can't compare languages just like that and things are not bad there's just better options / something along those lines.[/QUOTE]
[QUOTE=Svenskunganka;47689309]Anyone tried Golang yet? Seems very promising and just keeps on getting more and more popular. I really enjoy the syntax as well! Anyone found any caveats with it yet?[/QUOTE]
You could ask [URL="http://facepunch.com/member.php?u=296895"]Kaukassus[/URL], last I heard from him he was all over Go-lang.
[QUOTE=Moofy;47689932]You could ask [URL="http://facepunch.com/member.php?u=296895"]Kaukassus[/URL], last I heard from him he was all over Go-lang.[/QUOTE]
Cheers, I'll ask when he comes around :)
I'm trying to do animations with backgrounds using linear gradients, but they refuse to appear. Am I missing something, or..?
[URL="http://codepen.io/anon/pen/YXwQaE"]http://codepen.io/anon/pen/YXwQaE[/URL]
[URL="http://codepen.io/anon/pen/doGRez"]http://codepen.io/anon/pen/doGRez[/URL]
Sorry, you need to Log In to post a reply to this thread.