首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在heroku的nodejs应用程序中访问角路径

在heroku的nodejs应用程序中访问角路径
EN

Stack Overflow用户
提问于 2018-01-05 17:41:11
回答 2查看 86关注 0票数 0

我正试图在Heroku上主持一个angularjs项目。应用程序在本地和运行heroku local web时都能正常工作。

但是当尝试访问时,我得到了错误的TypeError: response.sendFile is not a function

Server.js

代码语言:javascript
复制
 //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

代码语言:javascript
复制
{
  "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.大写字母f的res.sendFile或小写f的res.sendfile没有工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-06 14:00:57

解决了。

我没有使用http.createServer(function (request, response) {}块,而是添加了app.listen(port);

票数 0
EN

Stack Overflow用户

发布于 2018-01-06 04:07:14

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

还请注意,sendFile在您的代码中出现了两次,所以您尝试在两处用sendfile替换它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48118658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档