node.js - Angular-webpack-Starter, 怎么把NodeJS添加进项目里?

查看:105
本文介绍了node.js - Angular-webpack-Starter, 怎么把NodeJS添加进项目里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如题,我clone了Angular-webpack-Starter项目,现在想把nodeJS作为后端来模拟数据,要怎么改呢?

解决方案

找到答案了!
config/webpack.dev.jsdevServer里添加

proxy: {
    '/api/*': 'http://<YOUR_BACKEND_HOST>:<YOUR_BACKEND_PORT>',
},

如:

devServer: {
      port: METADATA.port,
      host: METADATA.host,
      historyApiFallback: true,
      watchOptions: {
        aggregateTimeout: 300,
        poll: 1000
      },
      outputPath: helpers.root('dist'),
      proxy: {
        '/api/*': 'http://localhost:1234'
      }
},

对应的nodeJS代码:

const express = require('express');
const app = express();

app.get('/', function(req,res){
    console.log('got it');
    res.send().end();
});
app.get('/api/datas', function(req,res){
    console.log(req.baseUrl);
    res.send({datas: [1,2,3,4,5]}).end();
})
app.listen('1234',function(){
    console.log('running on 1234 port');
});

这篇关于node.js - Angular-webpack-Starter, 怎么把NodeJS添加进项目里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆