2

Why I can't use post method, error post not found (404)? I connected successful to database, I defined route endpoint POST in server.js and defined axios.post in Signup.vue but return post not found (404) in Signup.vue

//server.js
import express from 'express';
import bodyParser from 'body-parser';
import { MongoClient } from 'mongodb';
import { ServerApiVersion } from 'mongodb';

const app = express();
const port = 5173;

app.use(bodyParser.json());

const uri = "mongodb+srv://22022638:[email protected]/?retryWrites=true&w=majority&appName=MusibDB";

const client = new MongoClient(uri, {
  serverApi: {
    version: ServerApiVersion.v1,
    strict: true,
    deprecationErrors: true,
  }
});

async function connectToDatabase() {
  try {
    await client.connect();
    console.log("Connected to MongoDB");
  } catch (error) {
    console.error("Error connecting to MongoDB:", error);
  }
}
connectToDatabase();

app.post('/signup', async (req, res) => {
  try {
    const { username, password, email } = req.body;

    const database = client.db("musicApp");
    const collection = database.collection("users");

    collection.insertOne({ username, password, email });
    
    res.status(200).json({ message: "Đăng ký thành công" });
  } catch (error) {
    console.error("Đăng ký thất bại:", error);
    res.status(500).json({ message: "Đăng ký thất bại" });
  }
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
//Signup.vue
async signup() {
        try {
            await axios.post('/api/signup', {
                username: this.username,
                password: this.password,
                email: this.email,
            });
            alert('Đăng ký thành công');
            this.$router.push('/listfavour');
        } catch (error) {
            alert('Đăng ký thất bại');
        }
    }

I checked by postman but return 404. I'm new to StackOverFlow, please forgive me and help me solve the issue. Thank you so much!

1 Answer 1

1

Firstly, you are attempting to call the /api/signup route, but you have only defined a route for /signup.
Secondly, you cannot directly invoke /api/signup using axios unless you have previously set the baseURL. See the documentation for further details: https://axios-http.com/docs/config_defaults

5
  • I added axios.defaults.baseURL = ' localhost:5173'; but the error still hasn't appeared Commented Apr 8, 2024 at 15:38
  • 1
    Have you fixed the first problem I mentioned?
    – Baragouin
    Commented Apr 8, 2024 at 16:02
  • yes, i fixed the first issue you gave and added baseURL but the error still doesn't appear Commented Apr 9, 2024 at 6:23
  • I see other problems: You call the asynchronous connectDatabase function without taking into account the return value (Promise). You need to add wait in front or then/catch behind. Then express routers do not support an asynchronous callback function, you must manage asynchronous via then/catch and the connection will be waiting until the response is sent to the client.
    – Baragouin
    Commented Apr 9, 2024 at 8:17
  • Have you defined the baseURL before listening to the port ?
    – Baragouin
    Commented Apr 9, 2024 at 8:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.