node.js - Searching for public folder in routes path -
whenever different route visited other root, express not able find public folder containing stylesheets , javascript files, instead searches assets inside route path. appreciated.
error when visiting http://localhost:3000/users/login/ gives -
get /users/login/stylesheets/bootstrap.css 404 14.872 ms - 10418
/users/login/stylesheets/style.css 404 7.134 ms - 10418
/users/login/javascripts/bootstrap.js 404 57.890 ms - 10418
/users/login/javascripts/bootstrap.js 404 5.326 ms - 10418
app.js
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var app = express(); // modules var hbs = require('hbs'); var defaultlogger = require('express-logger'); var index = require('./routes/index'); var users = require('./routes/users'); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hbs'); app.set('view options', {layout: './layouts/application'}); hbs.registerpartials(__dirname + '/views/layouts/partials'); app.use(express.static(path.join(__dirname, 'public'))); app.use(defaultlogger({path: "log/development.log"})); //logger app.use(favicon(path.join(__dirname, 'public', 'bulb.ico'))); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser()); app.use('/', index); app.use('/users', users); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handler app.use(function(err, req, res, next) { // set locals, providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
uses.js
var express = require('express'); var router = express.router(); var userservice = require('../services/users') /* users listing. */ router.get('/', function(req, res, next) { userservice.all(function(users){ res.render('users/dummy') }) }); // login form router.get('/login', function(req, res, next){ res.render('users/login', {title: 'login'}); }); module.exports = router;
i guess path in format: <link href="stylesheets/bootstrap.css">
you should contain /
@ start of path. load correct path. <link href="/stylesheets/bootstrap.css">
Comments
Post a Comment