I'm using AngularJS and ExpressJS and having an issue with routing. I saw many other posts but none of those solutions seemed to work. Here is my routes in Express:
module.exports = function(app, auth) {
//Api routes
var mycontroller = require('../app/controllers/mycontroller');
app.get('/api/dostuff/:id', mycontroller.getBlockByHash);
//Home route
app.get("/", function(req, res) {
res.render('index');
});
};
When I go to my root /
, everything works as expected. ExpressJS serves up my index and angular picks up the rest. When I click a link /blocks
, it works as expected since AngularJS picks up the route. But when I refresh, I get a 404 not found error.
I tried app.get('*'
instead, but that gives me a completely different error where nothing loads.
I'm using Jade
to create the basic page structure with Express. My Express config is:
app.use(express.favicon());
app.use(express.static(config.root + '/public'));
Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (eg index.html)
The error you got there is express serving html into script tags and the browser fails to parse them as a valid javascript.
Use express.static
to serve static assets and then use app.get('*',
for redirecting all other requests to your angular.js entry point (index.html).
express.static
must be declared before app.router
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.