成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

node.js - Angular-webpack-Starter, 怎么把NodeJS添加進(jìn)項(xiàng)目里?

瀏覽:132日期:2023-10-09 10:55:08

問(wèn)題描述

如題,我clone了Angular-webpack-Starter項(xiàng)目,現(xiàn)在想把nodeJS作為后端來(lái)模擬數(shù)據(jù),要怎么改呢?

問(wèn)題解答

回答1:

找到答案了!在config/webpack.dev.js的devServer里添加

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’ }},

對(duì)應(yīng)的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’);});

標(biāo)簽: web