简体   繁体   中英

Openshift Node.js app serves javascript but not css

I am running a Node.js application with Express, however I cannot load my css from my stylesheets directory. Every request I make for css comes back with a 503 error. However all of my javascript loads fine. I tried moving the css file into the javascripts folder and changing the href in the html but every time I try to load it, the server will crash and node will restart.

I've verified that it's the CSS by removing the link from my index.html and the whole site works, just with no styling on it. If I run the application locally the CSS loads fine, the CSS issue only happens on the deployed Openshift instance.

app structure

server.js
node_modules/
public/
  --index.html
  --bower_components/
  --stylesheets/
    --app.css
  --javascripts/
    --app.js

server.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 Promise = require('bluebird');
var http = require('http');

var app = express();

app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('node-compass')({mode: 'expanded'}));
app.use(express.static(path.join(__dirname, 'public')));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;

var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';

var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(server_port, server_ip_address, function() {
    console.log("Listening on " + server_ip_address + ", server_port " + server_port);
});

io.sockets.on('connection', function(socket) {
    socket.emit('message', {'message': 'connected'});
});

require('./routes/weather')(io);

index.html

<!-- Stylesheets -->
<link rel="stylesheet" href="/stylesheets/app.css" type="text/css"/>

node-compass was interfering with the provided css and causing the error to be thrown. After commenting out

// app.use(require('node-compass')({mode: 'expanded'}));

the server served up the css just fine.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM