heroku静态资源nodejs

时间:2018-05-19 08:55:12

标签: javascript node.js express heroku

我在heroku上托管了我的nodejs项目并且它已成功部署,但是我无法从public或node_modules文件夹加载任何静态资源。在app.js中放置了一些代码行,但其他任何东西都没有用.Tried process.cwd()方法也是如此,但失败了。我是第一次这样做了。请帮助。

    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');
    var fs = require('fs');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    var indexRouter = require('./routes/index');
    var usersRouter = require('./routes/users');
    var app = express();
    var http = require('http').Server(app);
    process.env.PWD = process.cwd();
    // view engine setup
    app.use('/scripts', express.static(path.join(process.env.PWD, 'node_modules')));
    app.use('/jsonfiles', express.static(path.join(process.env.PWD, 'jsonfiles')));
    app.set('views', path.join(process.env.PWD, 'views'));
    app.use(express.static(path.join(process.env.PWD, 'public')));

    app.set('view engine', 'ejs');

    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());


    app.use('/', indexRouter);
    app.use('/users', usersRouter);

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      next(createError(404));
    });
    http.listen(process.env.PORT || 8000, function() {
        console.log('listening on *:8000');
    });



    // error handler
    app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};

      // render the error page
      res.status(err.status || 500);
      res.render('error');
    });

    module.exports = app;

0 个答案:

没有答案