-1
我有一個客戶端ios應用程序,使用afnetworking2連接到一個Node.Js後端,應該使用來自後路由的json數據進行響應。當在本地主機上測試時,一切都很好,但是當我在heroku上進行實時部署時,應用會發送索引頁的整個html而不是json。我不知道如何解決這個問題,甚至不知道爲什麼要發送html。任何和所有的幫助表示讚賞。我喜歡stackoverflow社區!我的代碼列表如下:節點Js Returing完整的HTTP頁面,而不是JSON數據
app.js:
var express = require('express'),
routes = require('./routes/index'),
api = require('./routes/api'),
mobiTokenAuth = require('./routes/jwt'),
https = require('https'),
cons = require('consolidate'),
mustac = require('mustache'),
path = require('path'),
bodyParser = require('body-parser'),
nodemailer = require("nodemailer"),
templatesDir = path.resolve(__dirname + '/templates'),
emailTemplates = require('email-templates'),
mysql = require('mysql2');
morgan = require('morgan');
var app = module.exports = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', cons.mustache);
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: true}));
app.use(bodyParser.json());
app.use(morgan('combined'));
// Set content type GLOBALLY for any response.
app.use(function (req, res, next) {
res.contentType('application/json');
next();
});
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST', 'PUT', 'DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
next();
});
app.disable('x-powered-by');
app.get("/", function(req, res){
res.render('index');
});
app.get("/partials/:name", routes.partials);
var port = process.env.PORT || 3000;
app.listen(port, function(req, res) {
console.log("Listening on " + port);
});
app.post('/api/signup', function(req, res){
console.log(req);
res.contentType('application/json');
var data = {
json: "test to see if data variable goes through as json data"
}
res.json({data:data});
next();
});
目前返回:
<html lang="en" ng-app="myApp">
<head>
<base href='/'>
<body><div>Testing to see if html data is sent instead of json</div></body>
</html>
仍然沒有工作後,我做了更改,我所得到的是index.html頁面的html數據,而不是json –
你有任何其他建議,爲什麼服務器發送HTML? –