有谁知道如何成功地将一个vanilla Javascript应用程序转换为Node Express应用程序,以便它可以在Heroku上部署?

时间:2018-01-05 01:21:01

标签: javascript jquery node.js ajax heroku

我是编程新手,我有一个客户端Javascript应用程序,它利用AJAX请求与chatbot进行通信。我有隐藏的API密钥,当我在本地运行我的应用程序时,它可以工作,但是当我推送到Heroku时,即使我让Heroku认为我的应用程序是PHP版本,它也无法识别变量(因为它本身就是JS和Heroku不部署静态站点)。因此,我正在尝试将其配置为Node项目,以便我能够设置环境变量。

下面的完整描述,我希望它足够详细。感谢您的帮助。

我的package.json

  "name": "ask-randy",
  "version": "1.0.0",
  "description": "Capstone",
  "main": "lib/index.js",
  "scripts": {
    "start": "nodemon lib/index.js",
    "build": "./node_modules/webpack/bin/webpack.js",
    "test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/turingschool-examples/game-time-starter-kit-FEm1.git"
  },
  "author": "Ashish Vaid",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/turingschool-examples/quantified-self-starter-kit/issues"
  },
  "homepage": "https://github.com/turingschool-examples/quantified-self-starter-kit#readme",
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.7",
    "babel-preset-env": "^1.6.0",
    "chai": "^3.5.0",
    "chromedriver": "^2.27.2",
    "css-loader": "^0.26.0",
    "jquery": "^3.1.1",
    "mocha": "^3.1.2",
    "mocha-loader": "^1.0.0",
    "node-libs-browser": "^1.0.0",
    "node-sass": "^4.3.0",
    "nodemon": "^1.14.7",
    "sass-loader": "^4.1.1",
    "selenium-webdriver": "^3.0.1",
    "style-loader": "^0.13.1",
    "webdriverjs": "^1.7.5",
    "webpack": "^1.14.0",
    "webpack-dev-server": "^1.16.2"
  },
  "dependencies": {
    "dotenv": "^4.0.0",
    "env2": "^2.2.0",
    "express": "^4.16.2",
    "jsdom": "^11.5.1"
  }
}

我的index.html(生活在我应用的根目录中)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ask Randy</title>
  <link rel="stylesheet" href="./lib/stylesheets/styles.css">
<link href='https://fonts.googleapis.com/css?family=Lobster|Raleway:400,200' rel='stylesheet' type='text/css'>
  </head>

<header class="cf">
		  <nav>
			    <h1 class="logo">Ask Randy</h1>
		  </nav>
	</header>

<br>
  <body>
    <div class="text-container">

      <div class="container">
        <img src="./images/randy_profile.png" alt="Avatar">
        <p>I'm Randy the Ask Randy Chatbot, you can ask me anything you know what I'm sayin.</p>
        <span class="time-right"></span>
      </div>
    </div>



<br>
    <div class="wrapper748 center padding-40 tablet-padding">

  <!-- Comment Form Start -->
  <!-- <div class="row shadow padding-15 white"> -->

    <!-- <form class="form wire teal"> -->
      <textarea rows="4" cols="110" placeholder="say something..."></textarea>

      <!-- Submit Start -->
      <div class="row pull-left -margin padding-top-10">
        <!-- <span class="btn fill-transparent text-gray hover-disable -padding-left"></span> -->
        <button class="btn teal pull-right">Submit</button>     <button class="btn red pull-right" type="reset" value="Reset">Clear Chat History</button>

      </div>
      <!-- Submit End -->
    <!-- </form> -->

<div class="lawrence-randy">
  <img class="randy-pic" src="./images/lawrence_randy.png" alt="randy-pic">
</div>


<script type='text/javascript' src='config.js'></script>
<script src="main.bundle.js"></script>

  </body>
</html>

我的index.js(代替了典型的Node应用程序的server.js)。

var express = require('express')
var app  = express()
var path = require('path');
require('dotenv').config()
var appRoot = process.env.PWD
console.log(appRoot)

app.set('port', process.env.PORT || 3000)

app.get('/', function(req, res) {
    res.sendFile(appRoot + '/index.html');
});


if(!module.parent) {
  app.listen(app.get('port'), function() {
    console.log("App is running on " + app.get('port') + ".")
  })
}

// const myAppId = config.APP_ID
// const myKey = config.USER_KEY

const randyAjaxRequests = require('./ajax-requests/chat-requests')
const randyResponseHandlers = require('./response-handlers/chat-responses')
const randyEventListeners = require('./event-listeners/chat-listeners')


// export{myAppId, myKey}

我在返回AJAX之后正在做一些DOM操作,我知道在Node中执行Jquery时我必须做一些调整,但我甚至无法获得本地环境来渲染我的资产(CSS,JS)。

我在控制台中收到错误,如下所示:

ERROR in ./~/raw-body/~/iconv-lite/encodings/tables/gb18030-ranges.json
Module parse failed: /Users/vaidashi/turing/4module/projects/ask-randy/node_modules/raw-body/node_modules/iconv-lite/encodings/tables/gb18030-ranges.json Unexpected token (1:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:9)
    at Parser.pp$4.raise (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp.semicolon (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:581:61)
    at Parser.pp$1.parseExpressionStatement (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:966:10)
    at Parser.pp$1.parseStatement (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:730:24)
    at Parser.pp$1.parseBlock (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:981:25)
    at Parser.pp$1.parseStatement (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:709:33)
    at Parser.pp$1.parseTopLevel (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/acorn/dist/acorn.js:3098:39)
    at Parser.parse (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
    at /Users/vaidashi/turing/4module/projects/ask-randy/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
    at Storage.finished (/Users/vaidashi/turing/4module/projects/ask-randy/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
    at /Users/vaidashi/turing/4module/projects/ask-randy/node_modules/graceful-fs/graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:447:3)
 @ ./~/raw-body/~/iconv-lite/encodings/dbcs-data.js 106:37-76

打开Chrome开发者工具时出现错误: enter image description here

0 个答案:

没有答案