[英]Node GET request returns html, when I specify JSON?
I am using the angular-fullstack, and I am trying to do a simple get request. 我正在使用angular-fullstack,并且尝试执行一个简单的get请求。 All relevant code below: 以下所有相关代码:
main.controller.js main.controller.js
this.$http.get('/'+"example").success(function(res){
console.log(res);
});
index.js index.js
router.get('/:city', controller.yelp);
user.controller.js user.controller.js
export function yelp(req, res, next){
res.json({"foo": "bar"});
}
Yet, when I open up console in Google Chrome dev tools, I get the following: 但是,当我在Google Chrome开发者工具中打开控制台时,会得到以下信息:
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<base href="/">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- build:css(client) app/vendor.css -->
<!-- bower:css -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
<!-- endbower -->
<!-- endbuild -->
<!-- build:css({.tmp,client}) app/app.css -->
<link rel="stylesheet" href="app/app.css">
<!-- injector:css -->
<link rel="stylesheet" href="app/admin/admin.css">
<link rel="stylesheet" href="app/app.css">
<link rel="stylesheet" href="app/main/main.css">
<link rel="stylesheet" href="components/footer/footer.css">
<link rel="stylesheet" href="components/modal/modal.css">
<!-- endinjector -->
<!-- endbuild -->
</head>
<body ng-app="nightlifeApp">
<!--[if lt IE 7]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<div ng-view=""></div>
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-X');
ga('send', 'pageview');
</script>
<!--[if lt IE 9]>
<script src="bower_components/es5-shim/es5-shim.js"></script>
<script src="bower_components/json3/lib/json3.min.js"></script>
<![endif]-->
<!-- build:js({client,node_modules}) app/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="bower_components/lodash/dist/lodash.compat.js"></script>
<script src="bower_components/angular-validation-match/dist/angular-validation-match.min.js"></script>
<!-- endbower -->
<!-- endbuild -->
<!-- build:js(.tmp) app/app.js -->
<script src="app/app.js"></script>
<!-- injector:js -->
<script src="app/admin/admin.module.js"></script>
<script src="components/auth/auth.module.js"></script>
<script src="components/util/util.module.js"></script>
<script src="components/auth/auth.service.js"></script>
<script src="app/admin/admin.controller.js"></script>
<script src="app/account/account.js"></script>
<script src="app/admin/admin.router.js"></script>
<script src="app/app.constant.js"></script>
<script src="app/main/main.controller.js"></script>
<script src="app/main/main.js"></script>
<script src="app/account/settings/settings.controller.js"></script>
<script src="app/account/login/login.controller.js"></script>
<script src="components/auth/interceptor.service.js"></script>
<script src="components/auth/router.decorator.js"></script>
<script src="components/auth/user.service.js"></script>
<script src="components/footer/footer.directive.js"></script>
<script src="components/modal/modal.service.js"></script>
<script src="components/mongoose-error/mongoose-error.directive.js"></script>
<script src="components/navbar/navbar.controller.js"></script>
<script src="components/navbar/navbar.directive.js"></script>
<script src="app/account/signup/signup.controller.js"></script>
<script src="components/util/util.service.js"></script>
<!-- endinjector -->
<!-- endbuild -->
</body>
</html>
Edit: This appears to be my index.html page 编辑:这似乎是我的index.html页面
The issue came from the definition of the routes. 问题来自路线的定义。 The route.js code is the following: route.js代码如下:
'use strict';
import errors from './components/errors';
import path from 'path';
export default function(app) {
// Insert routes below
app.use('/api/messages', require('./api/message'));
app.use('/api/things', require('./api/thing'));
app.use('/api/users', require('./api/user'));
app.use('/auth', require('./auth'));
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get((req, res) => {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
}
Thus, by simply adding /api/users
to my request, I was able to be directed to the correct page. 因此,只需将/api/users
添加到我的请求中,就可以将我定向到正确的页面。 (Note: Otherwise, we serve index.html, which explains why I was getting index.html). (注意:否则,我们将提供index.html,这解释了为什么我得到index.html)。
The successful request would be as follows: 成功的请求如下:
this.$http.get('/api/users/test').success(function(res){
console.log(res);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.