未找到Node.js公共css文件404

时间:2014-04-15 08:54:15

标签: javascript html css node.js express

我正在学习node.js,并且我将公共CSS文件提供给一个URL时出错。 它适用于几乎每个页面,我在页面上,并从127.0.0.1/css/style.css加载css文件。 当URL为127.0.0.1/project/idProject时,它会尝试从127.0.0.1/project/css/style.css获取css文件。

// INCLUDE MODULES =======================================================
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var Twig = require('twig');
var twig = Twig.twig;
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var flash    = require('connect-flash');
var configDB = require('./config/database.js');

// Assets ================================================================
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.favicon(path.join(__dirname, 'public/images/favicon.ico'))); 
// Start mongoose
mongoose.connect(configDB.url);

// USER MANAGEMENT =======================================================
require('./config/passport')(passport); // pass passport for configuration
app.use(express.logger('dev')); // log every request to the console
app.use(express.cookieParser()); // read cookies (needed for auth)
app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
app.set('view engine', 'twig'); // set up twig for templating
app.use(express.session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash())

// ROUTES =======================================================
// Set authentication variable
app.use(function (req, res, next) {
    app.locals.login = req.isAuthenticated();
    next();
});
require('./app/routes.js')(app, passport);

//ERROR MANAGEMENT =======================================================
app.use(app.router);
app.use(function(req, res, next){
  res.status(404);

  // respond with html page
  if (req.accepts('html')) {
    res.render('errors/404.twig', { url: req.url });
    return;
  }
  // respond with json
  if (req.accepts('json')) {
    res.send({ error: 'Not found' });
    return;
  }
  // default to plain-text. send()
  res.type('txt').send('Not found');
});
/*app.use(function(err, req, res, next){
  // we may use properties of the error object
  // here and next(err) appropriately, or if
  // we possibly recovered from the error, simply next().
  res.status(err.status || 500);
  res.render('errors/500.twig', { error: err });
});*/

//SOCKET IO =======================================================
//Quand on client se connecte, on le note dans la console
io.sockets.on('connection', function (socket) {
    console.log("New connection");
});

// LISTEN SERVER =======================================================
server.listen(80);

关于如何解决这个问题的任何想法?

问候!

2 个答案:

答案 0 :(得分:3)

我尝试了在评论中看到的方法,因为它对我不起作用,我发布了一个有效的答案。

所有.css文件都是静态的,因此您必须将它们提供给客户端。但是,您不会将静态文件作为快速中间件提供。因此你必须添加它们。

app.use(express.static(__dirname, 'css'));

答案 1 :(得分:0)

嗨,这对我来说是个难题,但是在萨尔瓦多的帮助下,这是有可能的。

我要放入的唯一内容是所有代码,并且您在html中进行引用,您只需要将文件而不是html文件中的文件夹放入

  //The index.js code
    var express = require('express');
    const path = require ('path');
    //app va a ser mi servidor.
    var app = express();

    app.set('port', 3000)

    //app.use(express.static('./public'));
    //app.use(express.static( 'css'));
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(express.static(path.join(__dirname, 'css')));
    app.listen(app.get('port'), () => {
    console.log('localhost:3000')
    } );

this is the structure