我正在npm start上运行我的Angular Project,并希望它同时侦听端口3000(假设它会自动侦听api文件夹中的任何内容)。
无论如何,看起来好像是在尝试监听端口,但由于谁知道原因,它总是捕获错误
直接执行节点BLL.js来查看是否存在JSON输出就像一个咒语,但是让REST api工作似乎并不奇怪。
package.json
{
"name": "project-vas",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxyConfig.json",
"build": "ng build --env=prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
proxyConfig.json
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"changeOrigin": true
}
}
api / DBConnection.js
exports.config = function()
{
return config = {
user: 'username',
password: 'password',
server: 'localhost',
database: 'databaseName'
};
}
api / BLL.js
const express = require('express');
const path = require('path');
const sql = require('mssql');
const app = express();
const connection = require('./DBConnection');
let config = connection.config();
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/dist/ProjectName/'));
app.get('/*', (require, res) => res.sendFile(path.join(__dirname)));
app.get('api/usertypes', function (req, res) {
sql.connect(config, function (err) {
if (err) { console.log(err); }
else {
var request = new sql.Request();
request.query('sp_GETUSERTYPES', function (err, recordset) {
if (err) {
console.log(err);
}
res.send(recordset);
});
}
});
});
environment.ts
export const environment = {
production: false,
api:"http://localhost:3000",
};
只是JSON数组形式的预期存储过程输出。 但是“每次尝试在Postman上进行测试时,尝试代理到localhost:4200 / api / usertypes时都会发生错误”。