addwordform.addEventListener('submit', (event)=>{
event.preventDefault();
const formdata=new FormData(addwordform);
const word=formdata.get('addword');
const description =formdata.get('addiscription');
const worddata={
word,description,totalcount
};
console.log(worddata);
fetch(API_URL,{
method:'POST',
headers:{
'content-Type':'application/json'
},
body:JSON.stringify(worddata),
}).then(response=>response.json()).then( data =>{
console.log(data);
});
});
this is the client side javascript here API_URL="http://localhost:3005/word" and server side code is
const express= require('express');
const serveStatic = require('serve-static');
const datastore= require('nedb');
const app= express();
app.listen(3005,()=>{console.log("listening on :http://localhost:3005")});
app.use(serveStatic('public',{'index':['client.html']}));
const database=new datastore('database.db');
database.loadDatabase();
app.post('/word',(req,res)=>{
const data=req.body;
database.insert(data);
res.json();
});
i am using express a node framework and vanilla javascript for client side all i want is to post the data from a form that has an id=addwordform and i am using nedb a light weight database management in node .problem with it is the worddata that i am sending from client side is not getting in the server side "req" so i cant save it in database and ultimatly i cant "res" it back ?
If you're using express version >= 4.16
. Body parser comes bundled with express.
It parses incoming requests with JSON payloads and is based on body-parser.
It is not needed to use body-parser
. Here is the documentation
You just have to add this code before require your routes.
app.use(express.json());
Here is the stackoverflow original post.
Here is the express release
Here is the express commit