访问heroku上nodejs app中的角度路由

时间:2018-01-05 17:41:11

标签: angularjs node.js heroku

我正在尝试在 Heroku 上托管angularjs项目。应用程序在本地和运行heroku local web时运行良好。

但是在尝试访问时,我收到错误TypeError: response.sendFile is not a function

Server.js

 //Load HTTP module
var express = require('express');
const app = express();
var http = require("http");
var port = process.env.PORT|| 8000 ;
//Create HTTP server and listen on port 8000 for requests
http.createServer(function (request, response) {
   // Set the response HTTP header with HTTP status and Content type
response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body "Hello World"
  response.end('Hello World\n');
  response.sendFile('views/main.html' , { root : __dirname});
  console.log(__dirname);
}).listen(port);

// Catch all other routes and return the index file
app.use(express.static(__dirname + '/views'));

app.get('/',function(req, res){
  res.sendFile('views/main.html' , { root : __dirname});
});
console.log("\n\n\n"+__dirname+"Hello\n\n\n\n"); // prints /app in node 
console

console.log("\n\n"+process.version+"\n\n\n\n"); // v6.10.2

// Print URL for accessing server
console.log('Server running at '+port);

的package.json

{
  "name": "startup",
  "private": true,
  "devDependencies": {
    "@angular/cli": "^1.4.7",
    "@angular/compiler-cli": "^4.4.0",
    "bower": "1.8.0",
    "typescript": "^2.3.4",
    "graceful-fs": "^4.1.11",
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "^2.3.1",
    "gulp-cache": "^0.2.10",
    "gulp-connect": "^2.3.1",
    "gulp-filter": "^2.0.2",
    "gulp-imagemin": "^2.3.0",
    "gulp-jshint": "^1.12.0",
    "gulp-karma": "0.0.4",
    "gulp-load-plugins": "^0.10.0",
    "gulp-minify-css": "^1.2.0",
    "gulp-ng-annotate": "^1.0.0",
    "gulp-plumber": "^1.0.1",
    "gulp-rev": "^5.0.1",
    "gulp-rev-replace": "^0.4.2",
    "gulp-uglify": "^1.2.0",
    "gulp-useref": "^3.0.0",
    "gulp-util": "^3.0.6",
    "gulp-watch": "^4.2.4",
    "jasmine-core": "^2.7.0",
    "jshint": "^2.9.5",
    "jshint-stylish": "^1.0.0",
    "karma": "^1.7.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.1.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "lazypipe": "^0.2.4",
    "minimatch": "^3.0.4",
    "open": "0.0.5",
    "phantomjs-prebuilt": "^2.1.16",
    "rimraf": "^2.4.0",
    "run-sequence": "^1.1.1",
    "wiredep": "^2.2.2"
  },
  "engines": {
    "node": "6.10.2"
  },
  "dependencies": {
    "body-parser": "~1.5.2",
    "express": "^4.8.0",
    "gulp-bower-fix-css-path": "^0.1.1",
    "gzippo": "^0.2.0",
    "method-override": "~2.1.2",
    "mongoose": "^4.12.4"
  },
  "scripts": {
    "start": "gulp serve"
  },
  "description": "This project is generated with [yo angular generator](https://github.com/yeoman/generator-angular) version 0.16.0.",
  "version": "1.0.0",
  "main": "gulpfile.js",
  "directories": {
    "test": "test"
  },
  "repository": {
    "type": "git",
    "url": "1.0.0"
  },
  "author": "",
  "license": "ISC"
}

几点:  1.节点版本是“6.10.2”     2.快递版是“^ 4.8.0”     3. res.sendFile用大写的F或res.sendfile用小写的f,没什么用。

2 个答案:

答案 0 :(得分:0)

你在heroku上使用什么构建包?您是否有可能在heroku的npm缓存中使用旧版本的express?尝试heroku config:set NODE_MODULES_CACHE=false并推送更改以强制重建。

另请注意,代码中存在sendFile两次,因此您是否尝试在两个位置都将其替换为sendfile

答案 1 :(得分:0)

解决。

而不是http.createServer(function (request, response) {}阻止,我添加了app.listen(port);