cari

Rumah  >  Soal Jawab  >  teks badan

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

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

PHP中文网PHP中文网2785 hari yang lalu458

membalas semua(1)saya akan balas

  • 大家讲道理

    大家讲道理2017-04-17 15:07:40

    找到答案了!
    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');
    });
    

    balas
    0
  • Batalbalas