获取本地主机:4200/API 404(未找到)提交数据后



我下载https://github.com/singhdigamber/angular8meanstackangularmaterial并部署它。

但是,当我尝试保存数据并查看数据时,我总是得到获取http://localhost:4200/api 404(找不到(

将数据添加到DB错误图片获取数据到DB错误图片

Angular V8.0.0MongoDB v4.0.10nodejs v12.2.0

//app.js

let express = require('express'),
  path = require('path'),
  mongoose = require('mongoose'),
  cors = require('cors'),
  bodyParser = require('body-parser'),
  dataBaseConfig = require('./database/db');
// Connecting mongoDB
mongoose.Promise = global.Promise;
mongoose.connect(dataBaseConfig.db, {
  useNewUrlParser: true
}).then(() => {
    console.log('Database connected sucessfully ')
  },
  error => {
    console.log('Could not connected to database : ' + error)
  }
)
// Set up express js port
const studentRoute = require('./routes/student.route')
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cors());
// Setting up static directory
app.use(express.static(path.join(__dirname, 'dist/angular8-meanstack-angular-material')));

// RESTful API root
app.use('/api', studentRoute)
// PORT
const port = process.env.PORT || 8000;
app.listen(port, () => {
  console.log('Connected to port ' + port)
})
// Find 404 and hand over to error handler
app.use((req, res, next) => {
  next(createError(404));
});
// Index Route
app.get('/', (req, res) => {
  res.send('invaild endpoint');
});
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/angular8-meanstack-angular-material/index.html'));
});
// error handler
app.use(function (err, req, res, next) {
  console.error(err.message);
  if (!err.statusCode) err.statusCode = 500;
  res.status(err.statusCode).send(err.message);
});

我想您忘了导出API路由的获取功能。

您可以在StudentRoute文件中创建这样的路线。

var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
  return "Hello World";
})
router.post('/', function (req, res, next) {
  return "Hello World";
})
module.exports = router;````

相关内容

  • 没有找到相关文章

最新更新