Struggling to get my data to post - I'm getting a 404 error. I've looked around the forum's and I think it has something to do with my route. I'm not sure I understand the routes aspect and how that works exactly. My get function works fine and pulls the data from the database. Any input would be helpful, I've been stuck on this one for a bit now and not sure where my issue is. Thanks everyone!
I have a database called test with a collection called workorders in it (Not sure if that helps or not)
server.js
const express = require('express')
const connectDB = require('./db.js')
const itemModel = require('./models/item.js')
const cors = require('cors')
const app = express()
app.use(express.json())
app.use(cors())
connectDB()
app.get('http://localhost:3001', async (req, res) => {
const response = await itemModel.find()
return res.json({workorders : response})
})
app.post('http://localhost:3001', (req, res) => {
console.log(req.body)
})
app.listen(3001, () => {
console.log("app is running");
})
app.js (post function - Called onClick )
const postData = (e) => {
axios
.post("http://localhost:3001", {
customerName: 'Test',
jobDescription: 'Test',
jobPrice: 'Test',
})
.then((response) => {console.log(response.data)})
.catch((err) => {
console.log(err);
});
};
item.js (object schema)
const itemSchema = new mongoose.Schema({
customerName: String,
jobDescription: String,
jobPrice: String,
laborNotes: String,
laborType: String,
IPWO: String,
refWO: String,
materialStatus: String,
dateScheduled: Date,
woStatus: String,
notes: String
})
const itemModel = mongoose.model("workorders", itemSchema)
module.exports = itemModel
app.post()
is a route/path, not a full URL.app.get('/', async (req, res) => {...})
andapp.post('/', async (req, res) => {...})
. Your server is already listening on port3001
so you don't need to specify the full url, only relative to the web root of the server which will be/
.