heroku节点服务器将无法运行

时间:2018-10-30 12:40:33

标签: html node.js autodesk-forge

嗨,我用nodeJS制作了一个伪造查看器,并在本地运行,当我尝试在heroku上部署时,它在检查器中返回“无法获取”,它发送了“无法加载资源:服务器响应,状态为404 (未找到)”

index.js是我的根目录,当键入“ npm start”时它称为我的index.html

    var path = require('path');
const express = require('express');

const PORT = process.env.PORT || 3000;
const config = require('./config');
if (config.credentials.client_id == null || config.credentials.client_secret == null) {
    console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
    return;
}

let app = express();
app.use( express.static(path.join(__dirname, '/public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.json({ limit: '50mb' }));
app.use('/api/forge/oauth', require('./routes/oauth'));
app.use('/api/forge/oss', require('./routes/oss'));
app.use('/api/forge/modelderivative', require('./routes/modelderivative'));
app.use((err, req, res, next) => {
    console.error(err);
    res.status(err.statusCode).json(err);
});
app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

config.js,在这里我将日志记录信息放置到autodeskforge

    // Autodesk Forge configuration
module.exports = {
    // Set environment variables or hard-code here
    credentials: {
        client_id: process.env.FORGE_CLIENT_ID || 'xxxxxx',
        client_secret: process.env.FORGE_CLIENT_SECRET || 'xxxxxx',
        callback_url: process.env.FORGE_CALLBACK_URL || 'https://nodeforge.herokuapp.com/api/forge/callback/oauth'
    },
    scopes: {
        // Required scopes for the server-side application
        internal: ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'],
        // Required scope for the client-side viewer
        public: ['viewables:read']
    }
};

这是运行JS文件的“公共”目录中的index.html。

 <!DOCTYPE html>
<html>
<head>
  <title>Autodesk Forge</title>
  <meta charset="utf-8" />
  <!-- Common packages: jQuery, Bootstrap, jsTree -->
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
  <!-- Autodesk Forge Viewer files -->
  <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/style.min.css?v=v6.0" type="text/css">
  <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/viewer3D.min.js?v=v6.0"></script>
  <!-- this project files -->
  <link href="css/main.css" rel="stylesheet" />
  <script src="js/ForgeTree.js"></script>
  <script src="js/ForgeViewer.js"></script>
</head>
<body>

  <div class="container-fluid fill">
    <div class="row fill">
      <div class="col-sm-4 fill">
        <div class="panel panel-default fill">
          <div class="panel-heading" data-toggle="tooltip">
            Buckets &amp; Objects
            <span id="refreshBuckets" class="glyphicon glyphicon-refresh" style="cursor: pointer"></span>
            <button class="btn btn-xs btn-info" style="float: right" id="showFormCreateBucket" data-toggle="modal" data-target="#createBucketModal">
              <span class="glyphicon glyphicon-folder-close"></span> New bucket
            </button>
          </div>
          <div id="appBuckets">
            tree here
          </div>
        </div>
      </div>
      <div class="col-sm-8 fill">
        <div id="forgeViewer"></div>
      </div>
    </div>
  </div>
  <form id="uploadFile" method='post' enctype="multipart/form-data">
    <input id="hiddenUploadField" type="file" name="theFile" style="visibility:hidden" />
  </form>
  <!-- Modal Create Bucket -->
  <div class="modal fade" id="createBucketModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Cancel">
            <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">Create new bucket</h4>
        </div>
        <div class="modal-body">
          <input type="text" id="newBucketKey" class="form-control"> For demonstration purposes, objects (files) are 
          NOT automatically translated. After you upload, right click on
          the object and select "Translate".
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-primary" id="createNewBucket">Go ahead, create the bucket</button>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

localhost:3000

1 个答案:

答案 0 :(得分:0)

我看到您正在按照本教程材料进行操作,但是看来您所做的更改可能导致您遇到的问题。

首先,根本没有提到index.js文件,我们使用的是一个start.js文件,该文件用于初始化节点服务器,并调用npm start的时间,heroku也将调用此命令(如果您检查了包json文件,您将看到npm start正在调用节点start.js。

第二,我看到有人提到过ejs,但我认为本教程中没有提及过ejs,您是否需要或对html模板进行了一些更改才能使用ejs?

此外,最好不要显示您的API密钥,尤其是在Stack Overflow这样的地方,成千上万的开发人员都可以看到它,我建议禁用这些密钥并创建新的密钥,以避免其他用户使用您的信用。

我会说再次检查此部分http://learnforge.autodesk.io/#/environment/setup/nodejs_2legged

我们已经将本教程多次部署到Heroku上了,没有问题,通常可以按照预期的说明逐步按照说明进行操作以解决任何问题。您也可以始终下载项目的整个源并将其与您的源进行比较。