简体   繁体   中英

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. All relevant code below:

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"});
}

Yet, when I open up console in Google Chrome dev tools, I get the following:

<!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

The issue came from the definition of the routes. The route.js code is the following:

'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. (Note: Otherwise, we serve index.html, which explains why I was getting index.html).

The successful request would be as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM