score:0
const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const bodyParser = require('body-parser')
const app = express();
// Connection Mysql
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'dbusers',
});
connection.connect(err => {
if (err) {
console.log("conection error");
return err;
}
});
// Connection Express Body-Parser Cors
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello from the users server')
});
// Selectionner Tous Les Utilisateurs
app.get('/users', (req, res) => {
const selectQuery = 'SELECT * FROM userslist';
connection.query(selectQuery, (err, results) => {
if(err) {
res.send(err)
}
else {
res.json({data: results})
}
});
});
// Ajouter Un Nouveau Utilisateur
app.post('/users/add', (req, res) => {
let nom = req.body.nom;
let prenom = req.body.prenom;
let adresse = req.body.adresse;
let email = req.body.email;
let insertQuery ="INSERT INTO userslist SET ?"
const user={nom,adresse,prenom,email}
connection.query(insertQuery,user, (err, results) => {
if(err) {
console.log("insert error");
res.send(err)
}
else {
res.send({ error: false, data: results, message: 'user has been
updated successfully.' });
}
});
});
// Editer Un Nouveau Utilisateur
app.post('/users/update/:id', (req, res) => {
let id = req.params.id;
let nom = req.body.nom;
let prenom = req.body.prenom;
let adresse = req.body.adresse;
let email = req.body.email;
let updateQuery =`UPDATE userslist SET ? WHERE id=${id}`
const user={nom,adresse,prenom,email}
connection.query(updateQuery,user, (err, results) => {
if(err) {
console.log("insert error");
res.send(err)
}
else {
res.send({ error: false, data: results, message: 'user has been
updated successfully.' });
}
});
});
// Suprimer un Utilisateur
app.post("/users/delete/:id", (req, res) => {
let id = req.params.id;
let sql = 'DELETE FROM userslist WHERE id=?';
connection.query(sql, [id], (error, results, fields) => {
if (error)
console.error(error.message);
console.log("Deleted Row(s):", results.affectedRows);
res.json({error:false,data: results})
});
});
app.listen(4000, () => {
console.log('Users server worked on port 4000...')
});
Source: stackoverflow.com
Related Query
- how can i post data from reactjs form to mysql database
- I want to send the data from a request form to a database in the MySQL workbench from a javascript file in a ReactJs project, but I have no idea how
- How to send data from a form data to a database using ReactJS , Node.js and MySQL queries?
- How to post form data from reactjs to controller in mvc asp.net
- how can i POST data from ant design form into Django Backed?
- How can i pass data from ReactJS submit form to AdonisJS
- How can I send data from this reactjs form to a database?
- How can i get data or pass a data from one page to another after submitting a form in reactjs
- How can I post data from react to sql database
- How can i get array of object from this data for state and count in reactjs
- How to retrieve data from form using a POST route?
- how can I fetch data from useQuery hook after a form submission in Apollo?
- How to post data from ReactJS to NodeJS Express API
- How can I Fetch and display Mysql data into ReactJS front end with Node JS as backend?
- How to fetch data from MySQL after POST completes
- How can I get the right data from database with react
- ReactJS - How to re-render functional component after POST data to database
- reactJS How can I add a search filter for data from the db
- How to pass data received from database as props to another component in Reactjs
- How can I get one single unique data from Database for each one component?
- How can I get and store Data from Textfield in Form Container?
- How to post dynamic form data from modal using React.js
- How do I structure data for POST from form in ReactJS?
- how to fetch data without reloading the page from firebase database in reactjs
- How can I fetch data from API url into my reactgoogle pie chart in reactjs
- How can I map data from a firestore database in a useEffect hook
- How to filter a data array in reactjs from firestore database
- how to control the img attributes src in fetching the data from database in Reactjs
- How can I pass the data from a POST request to my global state with Redux?
- how can you create an app.use() to attach data from database to res object
More Query from same tag
- Cannot return const from render ReactJs
- How to solve type issue when implementing React children components to have only specific components?
- font download url is changing when i change page?
- Moving data between react components
- Unable to batch.delete documents of a subcollection
- React unique "key" error
- ReactCSSTransitionGroup animate table cells
- Typescript: function with argument of enum type assigned to callback with argument type of string
- React JSX: Maximum update depth exceeded
- How do I access the last path in the current component? (React router V6)
- How to resize the images to fit in fixed size div?
- Maximum call stack exceeded error
- Redux-form: update/change two fields values at once
- How do I output multiple values(discounts)?
- React useEffect hook load onsnapshot conditionally
- React Redux : TypeError: store.getState is not a function
- Changing the location of the button in the 'react-sidebar' library
- React - can custom functions within a Class be split to a different file?
- Cant get my checkbox value in a variable using state in react
- Cors error strict-origin-when-cross-origin simple nodeJS-reactJS project
- How to pass data from the dialog box to a table defined in parent component.?
- A spread argument must either have a tuple type or be passed to a rest parameter React
- Multiple React apps in one shell
- React doesn't update the view even when Redux state is changed
- How to trigger rerender on test component?
- Is there a way to check if React's `StrictMode` is on?
- Hide or show element of a component based on role permission in react
- onClick event fired before clicking in react
- React Forms data is present in two components
- JavaScript filter does not update the React local state