gpt4 book ai didi

mysql - 将 Angular 应用程序连接到服务器

转载 作者:行者123 更新时间:2023-11-29 07:23:29 25 4
gpt4 key购买 nike

我是 Angular 的初学者,我用我的数据库 Mysql 创建了带有 Rest Crud API 的 Nodeserver,我想将它与我的项目 angular (ngx nebular) 相关联,但我做不到,谢谢你帮助我

这是 app.js

 var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var cors = require('cors');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var user=require('./model/user');
var api = require('./routes/api');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/user',api);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

`

最佳答案

您应该监听所有 GET 请求并仅将您的 index.html 作为文件发送,该文件实际上应该包含您的 Angular SPA。像这样:

app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

您可以查看 Express 文档:http://expressjs.com/fr/api.html#res.sendFile

关于mysql - 将 Angular 应用程序连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55061223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com