簡體   English   中英

當我指定JSON時,節點GET請求返回html?

[英]Node GET request returns html, when I specify JSON?

我正在使用angular-fullstack,並且嘗試執行一個簡單的get請求。 以下所有相關代碼:

main.controller.js

this.$http.get('/'+"example").success(function(res){
        console.log(res);
    });

index.js

router.get('/:city', controller.yelp);

user.controller.js

export function yelp(req, res, next){
  res.json({"foo": "bar"});
}

但是,當我在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>

編輯:這似乎是我的index.html頁面

問題來自路線的定義。 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'));
    });
}

因此,只需將/api/users添加到我的請求中,就可以將我定向到正確的頁面。 (注意:否則,我們將提供index.html,這解釋了為什么我得到index.html)。

成功的請求如下:

this.$http.get('/api/users/test').success(function(res){
        console.log(res);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM