I want each route containing the phrase Privacy to be rendered the same.
So I used http://forbeslindesay.github.io/express-route-tester to make sure:
/(.*)Privacy(.*)
Should work.
However - the following code still gets me 404 on valid examples:
/Privacy
/aaaPrivacy
/aaaPrivacyaa
var express = require('express');
var router = express.Router();
router.get('/(.*)Privacy(.*)', function(req, res) {
res.render('privacy', {'isPrivacy': true});
});
module.exports = router;
Related
I am playing with node.js and I don't quite understand why something I set up is working in one instance but if I make a slight change it will not work in another instance.
in my app.js I have
app.use('/musicplayer', require('./routes/music/index'));
in my music\index.js I have
var express = require('express');
var router = express.Router();
router.use('/users', require('./users'));
module.exports = router;
in my users.js I have this - working version
var express = require('express');
var usersRouter = express.Router();
var sqllite3 = require('sqlite3').verbose();
usersRouter.get('/login', function(req, res, next) {
res.render('music/login', { title: 'Express' });
});
module.exports = usersRouter;
But I would like to encapsulate the routes I am defining into another function like this not working this just hangs the page.
Modified version of my users.js not working
var express = require('express');
var usersRouter = express.Router();
var sqllite3 = require('sqlite3').verbose();
var router = function () {
usersRouter.get('/login', function (req, res, next) {
res.render('music/login', {title: 'Express'});
});
return usersRouter;
}
module.exports = router;
In the console I can see it comes in tries the get and nevers gets routed I see this "GET /musicplayer/users/login - - ms - -".
I have even put a console.log right before the return in the anonymous function I created to know it is getting in there and that I am hooking the pathways up right from the parent routes. And I do hit that log action to the screen.
Any help or tips would be appreciated:)
PS in case you are wondering I am trying to separate out apps for different development work I want to play with. So that is why I am doing the sub routing with musicplayer/index.js instead of just putting everything in the app.js for declaring of my main routes.
Router.use() expects an instance of another Router. However your (non-working) module only returns a function.
Use this in your index.js to fix the issue:
router.use('/users', require('./users')());
I have used the express generator plugin and I had all my routes generated on routes/index.js but I'm doing a refactor now and I'm putting all of the routes in it's respective router files. The thing is that the 'pg' module works fine if I put my code on the index.js :
index.js
var express = require('express');
var router = express.Router();
var pg = require('pg');
var connectionString = 'postgres://postgres:postgres#localhost:5432/dataDB';
router.use('/api/events', require('./events'))
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
Now if I request a route from the routes/events.js file I get a 'pg'(postgres driver) not defined
var express = require('express');
var router = express.Router();
var pg = require('pg');
var connectionString = 'postgres://postgres:postgres#localhost:5432/dataDB';
//get all events from a user
router.get('/user/id/:id_user', function(req, res) {
.....
});
router.post('/friends/', function(req, res) {
........
});
module.exports = router
And the app.js only includes the router/index.js file ......How can I solve this?. The requests are getting to the router/events.js file correctly, but it's just not recognizing the 'pg' module require......Thank you very much
The problem was that I was including
var pg = require('pg');
in both routes/index.js and in routes/events.js
I removed that line from the index.js and left it only in event.js and now it works perfectly. That require is going in the model layer anyway, but now that I fixed it I would like to know why requiring something on one script and then requiring something on another script fails on express given the fact that I included express and the router in both files and those 2 lines didn't fail but the mentioned line did......
So basically I want my code being able to go mywebsite.com/username and itll take them to their profile.
My app.js has -
var user = require('./routes/user');
app.use('/*', user);
and then in my user.js I have
router.get('/:username', function(req, res) {
console.log('the user name', req.params.username);
})
If I change router.get to just router.get('/') my console.log logs out undefined and if I leave it as router.get('/:username') I get a 404.. I also tried doing app.use('/:username', user) as well but that still didn't work.
You should't need to have a wildcard like you do on app.use
Try something like this.
Also see the express router documentation http://expressjs.com/en/guide/routing.html
// Required Moduiles
var express = require('express');
var router = express.Router();
var app = express();
// Add route to get
router.get('/:username', function(req, res) {
res.status(200).send(req.params.username)
})
// Add username
app.use('/', router)
// Start express server
app.listen(3030);
Update -
Just using app.use(router) would do the same thing if all your routes are going to be contained in this new router instance / middleware you are creating in a separate file.
I have a full CRUD API defined in Express, and I'd like to remove the duplication of base and use the snazzy route function, but I fear it's not possible.
Current:
var router = express.Router();
var base = '/api/foo/bar/';
router.get(base, this.list);
router.get(base + ':id', this.read);
router.post(base, this.create);
router.put(base + :id', this.update);
router.del(base + :id', this.del);
Desired:
var router = express.Router();
router.route('/api/foo/bar')
.get(this.list)
.get(':id', this.read)
.post(this.create)
.put(':id', this.update)
.del(':id', this.del)
The problem is that the verb functions (get, post, put, del) do not accept a string as their first parameter.
Is there a similar way to achieve this?
Important: Using this technique will work, but we aware that as of Express 4.3.2, all subroutes defined on the nested router will not have access to req.params defined outside of it, nor param middleware. It's completely quarantined. This, however, is subject to change in a later 4X version. See https://github.com/visionmedia/express/issues/2151 for more (up to date) info.
How about this instead:
// api.js
var router = express.Router();
router
.route('/')
.get(this.list)
.post(this.create);
router
.route('/:id')
.get(this.read)
.put(this.update)
.del(this.del);
module.exports = router;
// app.js / main.js
app.use('/api/foo/bar', require('./api'));
or if you want to chain all of them at once:
// api.js
var router = express.Router();
router
.get('/', this.list)
.get('/:id', this.read)
.post('/', this.create)
.put('/:id', this.update)
.del('/:id', this.del);
module.exports = router;
// app.js / main.js
app.use('/api/foo/bar', require('./api'));
I have the following files
lib/pub
lib/pub/index.js
app.js
On App.js
I have:
// app.js
var express = require("express")
, app = express()
, router = express.Router()
;
...
router.use('/pub',require('./pub'));
and then on index.js
// pub/index.js
var express = require('express')
, router = express.Router()
;
console.log("file loaded successfully")
module.exports = function(){
router.get('/',function(req,res){
console.log("got the get request")
})
}
The problem I have when I do localhost/pub request, I never get the got the get request, no matter whatever I try to change the code around, trying to add pub to the path.
router.get('/',...
router.get('/pub',...
router.get('./pub,...
router.get('./',...
router.get('pub',...
etc...
None of those or any other silly way I have attempted work... I can never get the log to say yes I got the request...
What am I doing wrong ! (expressjs changes so frequently and radically, any web tutorials become redundant or any previous help others got)
(edited to reflect comments)
If you want to move your routes to an external file, use the following pattern:
app.js
var express = require('express');
var app = express();
require('./routes')(app);
routes.js
module.exports = function(app) {
app.get('/pub', function(req, res) {
console.log('got the get!');
res.end();
});
};