简体   繁体   中英

POST http://localhost:3000/api/login 404 (Not Found)

I have been configuring my database that is linked with nodejs in the same folder, to be able to advance a little more as a programmer, and I try to make an ajax request to the database, but it gives me an error as if the database was off or the url was wrong

  POST http://localhost:3000/api/login 404 (Not Found)

This is the petition code

userLogin = (username, password) =>{
    const url = 'http://192.168.1.7:3000/api/login'
    console.log('hola')
    fetch('/api/login', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({username: username, password: password})
    })
    .then(r => r.json())
    .then(data =>{
      if(data.status === 200){
        console.log('hola')
        this.setState({
          status: data.status,
          message: data.message,
          username: username
        })
        this.userSearch()
      }
    })
  }

Routes:

const express = require('express');
const api = express.Router();
const controllers = require('../controllers/controllers')
const cors = require('cors')



api.post('/register', controllers.registerUser)
api.post('/login', controllers.loginUsers)
api.get('/test',  controllers.test)

module.exports = api;

I have already made ajax requests before but the time I do it within the same node.js, if someone knows how to solve it please help me

var app = express();
const controllers = require('../controllers/controllers')
const cors = require('cors')
var bodyParser = require('body-parser');
var port = process.env.PORT || 3013;

app.use(bodyParser.json());
app.use(cors(corsOption));

var server = require('http').Server(app);

server.listen(port, function () {
    console.log('Updated : Server listening at ports', port);
});

app.post('/api/register', controllers.registerUser)
app.post('/api/login', controllers.loginUsers)
aapppi.get('/api/test',  controllers.test)

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