ПОЛУЧИТЬ localhost: 4200 / api 404 (не найдено) после отправки данных - PullRequest
0 голосов
/ 09 июля 2019

Я загружаю https://github.com/SinghDigamber/Angular8MeanstackAngularMaterial и внедряю его.

Но хотя я пытался сохранить данные и просмотреть их, я всегда получаю GET http://localhost:4200/api 404 (не найден))

добавить данные к изображению ошибки дБ получить данные к изображению ошибки дБ

Angular v8.0.0 mongoDB v4.0.10 nodejs 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);
});

1 Ответ

0 голосов
/ 09 июля 2019

Я думаю, вы забыли экспортировать функции get и post для ваших маршрутов 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;````
...