简体   繁体   中英

POST http://localhost:4200/ 404 Not found (Request from Angular to Nodejs)

I want to know what I am doing here:

What is the meaning of the error that I am getting here? What does it indicate? What is the way to solve this?

在此处输入图像描述

app.js from node.js

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const cors = require("cors");

var corsOptions = {
};

var routesApi = require('./app_api/routes/index');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

require('./app_api/models/db')

var routesApi = require('./app_api/routes/index');

app.use('/api', routesApi)
app.use('/hello', routesApi)
app.use('/', routesApi)

module.exports = app;

app_api/routes/index.js

var express = require('express')
var router  = express.Router()

var ctrlLocations = require('../controllers/locations')
var ctrlReviews   = require('../controllers/reviews')

router.post('/', ctrlLocations.helloCreate);

module.exports = router

hello-service.service.ts from Angular

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

const baseUrl = 'http://localhost:4200';

@Injectable({
  providedIn: 'root'
})
export class HelloServiceService 
{
  constructor( private http: HttpClient ) { }

  create( data: {} ) 
  {
    console.log("data:", data)
    return this.http.post(baseUrl, data);
  }

}

First looks like you are missing a statement in your nodejs app that specifies the port to listen to. Something like:

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Second you don't want to send a POST request to http://localhost:4200 because it is your frontend angular app and does not support POST request, which is why you are getting 404. You want to send request to your node app rather which in the example above will be http://localhost:3000 . Try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM