简体   繁体   English

提供静态文件的openshift express应用

[英]openshift express app serving static files

Noob here. 菜鸟在这里。 I'm trying to build an api server on Openshift using express. 我正在尝试使用express在Openshift上构建api服务器。 I also want to be able to serve static files from the /static folder. 我还希望能够从/ static文件夹提供静态文件。 The problem is, I can't figure out how to set it up on Openshift. 问题是,我不知道如何在Openshift上进行设置。 I've tried everything I can think of. 我已经尝试了所有我能想到的。

I have 2 server files, the app setup which calls a router.js file for the routes. 我有2个服务器文件,该应用程序设置为路由调用router.js文件。

app.js app.js

    const express = require('express');
    const http = require('http');
    const bodyParser = require('body-parser');
    const morgan = require('morgan');
    const env = process.env;
    const app = express();
    const router = require('./router');
    const mongoose = require('mongoose');
    const cors = require('cors');


    // DB Setup
    // default to a 'localhost' configuration:
    var connection_string = '127.0.0.1:27017/api:api';
    // if OPENSHIFT env variables are present, use the available connection info:
    if(env.OPENSHIFT_MONGODB_DB_PASSWORD){
      connection_string = env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
      env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
      env.OPENSHIFT_MONGODB_DB_HOST + ':' +
      env.OPENSHIFT_MONGODB_DB_PORT + '/' +
      env.OPENSHIFT_APP_NAME;
    }
    mongoose.connect('mongodb://' + connection_string);


    // App Setup
    app.use(morgan('combined')); //logging middleware
    app.use(cors()); // allow cross origin requests
    app.use(bodyParser.json({ type: '*/*'}));  //read requests as json

 ----->  do I need to put something here ????

    router(app);


    // Server Setup
    const port = env.NODE_PORT || 3090;
    const ip = env.NODE_IP || 'localhost';
    const server = http.createServer(app);
    server.listen(port, ip);
    console.log('Server listening on: ', port);

router.js router.js

const Authentication = require('./controllers/authentication');
const passportService = require('./services/passport');
const passport = require('passport');
const requireAuth = passport.authenticate('jwt', { session: false});
const requireSignin = passport.authenticate('local', { session: false});

module.exports = function(app) {

 app.post('/signup', Authentication.signup);
 app.post('/signin', requireSignin, Authentication.signin);

 app.get('/health', function (req, res, next ) {
      res.writeHead(200);
    res.end();
 });

 ---->  and/or something here?
}

Everything works except serving static files. 除提供静态文件外,一切正常。 Not sure if I need to put something in the app.js file as middleware, in the router file or both. 不知道我是否需要在app.js文件中放置一些东西作为中间件,在路由器文件中或在这两者中放置。 Also not sure if I need to use Openshift environment variables? 还不确定我是否需要使用Openshift环境变量? Can someone nudge me in the right direction? 有人可以向正确的方向推动我吗?

In express you can serve static files by adding the following to your app.js using express.static() 在express中,您可以通过使用express.static()将以下内容添加到app.js来提供静态文件

var path = require('path');

// put this before all of your routes
app.use(express.static(path.join(__dirname, 'static')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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