【问题标题】:AngularJs Server Node js /getAngularJs 服务器节点 js /get
【发布时间】:2016-09-15 02:01:33
【问题描述】:

您好,目前我的 Angular 项目中有一个带有 httpbackend 的假后端。但是我想在服务器节点js中传输我的假后端但我不知道。

所以此刻我有这个:

var express = require('express')
    , path = require('path')
    , fs = require('fs')
    , bodyParser = require('body-parser')
    , morgan = require('morgan');

var apps = express();
var staticRoot = __dirname + '/';
apps.set('port', (process.env.PORT || 3000));
apps.use(express.static(staticRoot));
apps.use(bodyParser.urlencoded({ extended: false }));
apps.use(bodyParser.json());
apps.use(morgan('dev'));

apps.use(function (req, res, next) {

    var ext = path.extname(req.path);
    if (ext !== '') {
        return next();
    }
});



apps.get('/getTpl', function (req, res) {

    res.writeHead(200);
    res.end(JSON.parse(["tp1", "tp2", "tp3", "tp4", "tp5", "tp6", "tp7"]));

});

apps.listen(apps.get('port'), function () {
    console.log('serveur en route, port : ', apps.get('port'));
});

我的控制器:

 ctrl.tpls = [];
        ctrl.tplJson = undefined;

        diapoService.getTpl().then(function (response) {
            ctrl.tpls = JSON.stringify(response.data);
            console.log(response.data);
        });

   function getTpl() {
            return $http({
                method: 'GET'
                , url: '/getTpl'
            });

我想在我的选择中发送我的数组,但我的选择是空的,为什么?请

非常感谢您的回答

【问题讨论】:

  • 而不是 res.writeHead 和 res.end 使用 res.json(JSON.parse(....));

标签: angularjs json node.js


【解决方案1】:

你必须改变这个函数:

apps.get('/getTpl', function (req, res) {
  res.status(200).json(["tp1", "tp2", "tp3", "tp4", "tp5", "tp6", "tp7"]);
});

另外我不使用 Angular,但我认为您不必对响应进行字符串化:

diapoService.getTpl().then(function (response) {
    ctrl.tpls = JSON.stringify(response.data); // <- Is this necessary?
    console.log(response.data);
});

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 2018-10-24
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2016-06-20
    相关资源
    最近更新 更多