简体   繁体   English

heroku节点服务器将无法运行

[英]heroku node server won't run

Hi i made a forge viewer with nodeJS and run locally, when i try to deploy on heroku it return "cannot get" in the inspector it sends "Failed to load resource: the server responded with a status of 404 (Not Found)" 嗨,我用nodeJS制作了一个伪造查看器,并在本地运行,当我尝试在heroku上部署时,它在检查器中返回“无法获取”,并发送“无法加载资源:服务器响应状态为404(未找到)”

index.js is my root when is type "npm start" it calls my index.html 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, here where i put my logging informations to autodeskforge 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']
    }
};

and here is my index.html in a "public" directory who run the JS files. 这是运行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>

本地主机:3000

I see you are following the tutorial material, but it seems you have made changes that could be causing the issues you are experiencing. 我看到您正在按照教程材料进行操作,但是似乎您进行了更改,可能会导致遇到问题。

First, there is no index.js file mentioned at all, what we use is a start.js file that initializes the node server and the time npm start gets called and heroku will call this command as well, if you check the package json file you will see npm start is calling node start.js. 首先,根本没有提到index.js文件,我们使用的是一个start.js文件,该文件用于初始化节点服务器,并调用npm start的时间,并且如果您检查package json文件,heroku也将调用此命令。您将看到npm start正在调用节点start.js。

Second, I see some mentioned of ejs and I don't think the tutorial has any mentioned of it, do you need or did some changes to the html template in order to use ejs? 其次,我看到有人提到过ejs,但我认为本教程中没有提及过ejs,您是否需要或对html模板进行了一些更改才能使用ejs?

Also, it is best practice to NEVER show your API keys, specially in a place like Stack Overflow where millions of developers will see it, I will recommend disabling those keys and create new ones to avoid other users to use your credits. 另外,最好不要显示您的API密钥,特别是在Stack Overflow这样的地方,成千上万的开发人员都可以看到它,我建议禁用这些密钥并创建新的密钥,以避免其他用户使用您的信用。

I will say check this section again http://learnforge.autodesk.io/#/environment/setup/nodejs_2legged 我会说再次检查此部分http://learnforge.autodesk.io/#/environment/setup/nodejs_2legged

We have deployed this tutorial many times to Heroku with no problem, and what usually fixes any issues is following along the instructions step by step like it was intended. 我们已经毫无问题地将本教程多次部署到Heroku上,并且按照预期的说明逐步按照说明进行操作,通常可以解决任何问题。 You can always download the entire source of the project as well and compare it to yours. 您也可以始终下载项目的整个源并将其与您的源进行比较。

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

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