i am trying to send data of the user from react to express and use the create function to create a new user
but is says error 404 and page not found
why?
my api endpoint (found in client/src/components/api-users :
async function create (data) {
try {
const resp = await fetch('/api/signup/' , { //error initiating here
method:'POST',
mode:"cors",
credentials:'include',
headers:{
'Content-Type': 'application/json ',
'Accept': 'application/json',
"Access-Control-Origin": "*"
},
body:JSON.stringify(data)
})
console.log(resp)
console.log(resp.body)
resp.headers.forEach(console.log);
return JSON.stringify(resp);
} catch (err) {
console.log(err)
}
}
export {
create
}
my user controller for signup:
var jwt = require('jsonwebtoken');
var atob =require('atob')
var Cryptr = require('cryptr')
var cryptr = new Cryptr('q1w2e3r4t5y6u7i8o9p0p0o9i8u6y5t4r3e2w1q')
var db =require('../server')
const create = (req, res, next) =>{
first_name = req.body.first_name,
last_name = req.body.last_name,
username =req.body.username,
password= req.body.password,
email=req.body.email,
dec_pass =atob(toString(req.body.password)),
encrypted_pass =cryptr.encrypt(dec_pass)
var sql = "INSERT INTO `user`(`user_id`,`first_name`,`last_name`,`username` , `email`,`password`) VALUES ('','" + first_name + "','" + last_name + "','" + username + "','" +email+ "','" +encrypted_pass+ "')";
var query = db.query(sql, function(err, result){
console.log(query)
console.log(req)
return (JSON.stringify(result));
});
};
export
{ create }
server js file for db connection:
var Sequelize = require('sequelize')
var app = require('./app')
var CONFIG= require('../config/CONFIG')
const db = {}
const sequelize = new Sequelize ("users" , "root" , "" ,{
host:'localhost',
dialect:'mysql',
operatorAliases:false,
pool:{
max:5,
min:0,
acquire:30000,
idle:10000
}
})
console.log(CONFIG.db_host)
db.sequelize=sequelize
db.Sequelize=Sequelize
console.log('alright')
export default db
mu user routes :
const express = require('express');
const router = express.Router();
var userCtrl = require ('../controllers/user.controller')
router.post('/signup', userCtrl.create)
module.exports = router
my signupjs react file
import React, {Component} from 'react'
import {create} from './api-user.js'
class SignUp extends Component {
constructor(){
super();
this.state = {
username:'',
first_name:'',
last_name :'',
email : '',
password :''
}
this.clickSubmit = this.clickSubmit.bind(this)
}
componentWillReceiveProps(nextProps) {
console.log("nextProps", nextProps);
}
componentDidMount(){
console.log("Component did mount")
}
handleChange = e => {
if (e.target.name === "username") {
this.setState({ username: e.target.value });
}
if (e.target.name === "first_name") {
this.setState({ first_name: e.target.value });
}
if (e.target.name === "last_name") {
this.setState({ last_name: e.target.value });
}
if (e.target.name === "email") {
this.setState({ email: e.target.value });
} if (e.target.name === "password") {
this.setState({ password: e.target.value });
}
}
clickSubmit = (e) => {
e.preventDefault()
const data = this.setState({
first_name :this.state.first_name,
last_name : this.state.last_name,
username : this.state.username,
password:this.state.password,
email:this.state.email,
})
create(data) //i dnt know if this correct or not
}
As #Deep Kakkar mentioned you don't set an api prefix so you should find /signup working instead of /api/signup
also fetch(/api/signup) will hit this relative path on your current domain (where react app is up on), you need to set full path instead, for instance, if your http-server is up on port 4000 and your react app is up on 3000 then you should fetch http://localhost:4000/api/signup not /api/signup as this will be http://localhost:3000/api/signup
Localhost:3000/api/products 404 Error
You did not create res.get("/api/products") on server.js or you did not set the proxy. check below for proxy setting.
Proxy error: could not proxy request /api/products
Check this:
frontend/package.json
{
"name": "frontend",
"proxy": "http://127.0.0.1:5000",
...
}
stop running frontend and backend
Run backend first
npm start
Then frontend
cd frontend
npm start
Related
I'm creating a simple React Native app, using...(React Native as FrontEnd, Node js as Server(without Express Framework) Database in MongoDB Local), so I Created Backend for the app, Server.js and React Native component in CreateUser.js
my problem is
When I click submit button for store data that time I got an Error and it was TypeError(Network Request Failed), I tried to call API with different IP Addresses also,
so please help me to how can I connect React Native with NodeJS,
CreateUser.js (frontEnd)
import React from 'react';
import { StyleSheet,
Text,
View,
TextInput,
TouchableOpacity
} from 'react-native';
class CreateUser extends React.Component {
constructor(){
super();
this.state ={
name:'',
email:'',
mobile:''
}
}
updateValue(text, field){
if(field == 'name'){
this.setState({
name:text,
})
}
else if(field == 'email'){
this.setState({
email:text,
})
}
else if(field == 'mobile'){
this.setState({
mobile:text
})
}
console.log(text);
}
submit(){
let collection = {}
collection.name=this.state.name,
collection.email=this.state.email,
collection.mobile=this.state.mobile
console.warn(collection);
console.log("submit btn pressed and collection is", collection);
// fetch code
var url = 'http://localhost:3005/save';
console.log("collections is that ===== ",collection)
fetch('url', {
method: 'post',
headers:{
'Content-Type': 'application/json',
'Accept':'application/json'
},
body: JSON.stringify(collection),
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(res => console.log('Success',res))
}
render(){
const {name, email, mobile} = this.state
return (
<View style={styles.container}>
<Text style={styles.header}>Insert User</Text>
<TextInput
placeholder="Enter Name"
style={styles.textinput}
onChangeText={(text) => this.updateValue(text, 'name')}
></TextInput>
<TextInput
placeholder="Enter Email"
style={styles.textinput}
onChangeText={(text) => this.updateValue(text, 'email')}
></TextInput>
<TextInput
placeholder="Enter Mobile"
style={styles.textinput}
onChangeText={(text) => this.updateValue(text, 'mobile')}
></TextInput>
<TouchableOpacity
style={styles.btn}
onPress={() => this.submit()}
>
<Text >Submit</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
regform:{
alignSelf:'stretch',
},
header:{
fontSize:24,
color:'#000',
paddingBottom:10,
marginBottom:20,
borderBottomColor:'#199187',
borderBottomWidth:1,
},
textinput:{
alignItems:'stretch',
height:40,
marginVertical:10,
marginHorizontal:10,
marginBottom:20,
color:'black',
borderBottomColor:'gray',
borderBottomWidth:2,
},
btn:{
alignSelf:'stretch',
alignItems:'center',
backgroundColor:'#59cbbd',
padding:20,
marginTop:30,
},
btntext:{
color:'#000',
fontWeight:'bold',
},
});
export default CreateUser;
server.js (backEnd)
// http module Node server
const { json } = require("body-parser");
const http = require("http");
const { parse } = require('querystring');
const app = http.createServer((req,res) =>{
const url = require('url');
const bodyParser = require('body-parser')
const mongoose = require('mongoose');
const User = require('./User');
var jsonParser = bodyParser.json();
const path = req.url;
console.log(req.url);
if(req.url ==="/save" && req.method == 'POST' ){
console.log("inside save API ") ;
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log(
parse(body)
);
res.end(body);
});
var MongoClient = require('mongodb').MongoClient;
var urlm = "mongodb://localhost:27017/userdb";
MongoClient.connect(urlm, function(err, db) {
if (err) throw err;
var dbo = db.db("userdb");
var jsonObj = JSON.parse(body);
var myobj = {body};
console.log("Post Data BODY is ",jsonObj);
dbo.collection("users").insertOne(jsonObj, function(err, res) {
if (err) throw err;
console.log("1 record inserted");
db.close();
});
});
}
}).listen(3005,()=>{
console.log('server is running on 3005 port');
});
The url parameter in the fetch function in your react native code is variable not a string, its url not 'url'
fetch(url, {
method: 'post',
headers:{
'Content-Type': 'application/json',
'Accept':'application/json'
},
body: JSON.stringify(collection),
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(res => console.log('Success',res))
}
I am learning React.js and got problem in a place.
I am fetching data from localhost:4000/products (i.e)
Here is my localhost:4000/products page
And I am doing the backend query in node.js
Here is the page:
const express = require('express');
const app = express();
const cors = require("cors");
const mysql = require("mysql");
app.use(cors());
const selectallquery = "SELECT * FROM users";
const db = mysql.createPool({
host: "localhost",
user: "root",
password: "Sakthi#1234",
database: "reactSql",
});
app.get("/",(req,res)=> {
res.send('Hello Sakthi')
});
app.get("/products/add", (req,res)=> {
const { name } = req.query;
console.log(name);
const insertquery = `INSERT INTO users (name) VALUES ('${name}')`;
db.query(insertquery, (err,result)=> {
if(err){
return res.send(err)
}
else{
return res.send("Successfully added...`")
}
});
});
app.get("/products", (req,res)=> {
db.query(selectallquery, (err,result)=> {
if (err){
return res.send(err)
}
else{
return res.send(result)
}
});
});
app.listen(4000,()=> {
console.log("Running on server 4000")
});
And here is my React.js page:
import React, { useState, useEffect } from 'react'
import axios from "axios";
function Login() {
const [names, setnames] = useState([])
useEffect(()=> {
axios.get("http://localhost:4000/products")
.then(res => {
if (res !== ""){
setnames([...names,res.data.name])
}
})
.catch(err => {
console.log(err)
})
},[])
return (
<div>
{
names
}
</div>
)
}
export default Login
The state names is not set with any value.
And it is not showing any values:
Its the page showing nothing :(
And Also i want to know how to make the useEffect hook not to render on first render(componentWillMount) and only to render at last(componentDidMount).
This is my console of res:
First, you need to update setnames because res.data is an array so you can not use res.data.name:
setnames(res.data)
And in return, you need to use map to show name:
{names.map((item) => (
<p key={item.id}>{item.name}</p>
))}
res.data is an array, so if you want to only keep names, you need to map on it to only store names to your state
setnames(names => [...names, ...res.data.map(({name}) => name)])
You can use useRef to set the node not to render at the first time.
Make React useEffect hook not run on initial render
I have a button that lauches a fetch to my API that uses KOA and JWT. The javascript for the fetch initiated on click is:
<script>
function loginButton(user, pass) {
fetch('http://localhost:5454/api/login', {
method: "post",
headers: {
'Content-Type': "application/json"
},
body: JSON.stringify({
username: user,
password: pass
})
})
.then( (response) => {
console.log("Success")
})
.catch(e => console.log(e));
}
</script>
The code for my Authentication is:
router.post(`${BASE_URL}/login`, async (ctx) => {
const reqUsername = ctx.request.body.username
const reqPassword = ctx.request.body.password
const unauthorized = (ctx) => {
ctx.status = 401
ctx.body = {
error: 'Invalid username or password'
}
}
let attemptingUser
try {
attemptingUser = await Employee.findOne({ where: { username: reqUsername }})
if (attemptingUser != null && attemptingUser.password === reqPassword) {
ctx.status = 200
ctx.body = {
username: attemptingUser.username,
given_name: attemptingUser.given_name,
role: attemptingUser.role,
created_at: attemptingUser.createdAt,
updated_at: attemptingUser.updatedAt,
}
const token = jwt.sign({ username: attemptingUser.username, role: attemptingUser.role }, SECRET)
ctx.set("X-Auth", token)
} else {
unauthorized(ctx)
}
} catch(err) {
console.error(err)
console.error(`Failed to find username: ${reqUsername}`)
unauthorized(ctx)
}
})
The code for my KOA initiation is:
require('dotenv').config()
const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const baseRoutes = require('./routes')
const cors = require('#koa/cors');
const PORT = process.env.PORT || 8080
const app = new Koa()
app.use(bodyParser())
app.use(baseRoutes.routes())
app.use(cors());
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`)
})
Im using Port 8080 for my http-server and port 5454 for my npm server. I am getting a Failed to Fetch in the catch of the Fetch, as well as a CORS error related to not having a Access-Control-Allow-Origin header in the response header. I've tried a couple things and am ready to have a new set of eyes look at it, any tips?
Edit: I am successfully receiving the token in the X-Auth header, but for some reason it’s still throwing errors and I’d like to get them resolved before it spirals out of control.
I am trying to submit a couple text boxes to my SQL database using React js through a post request, then on the server side I am listening for the post and querying the data to my database. Right now both values are returning "undefined" regardless of what the user inputs into the form, so I think I've screwed up the React side code. This is the code I am using
Server.js:
var express = require('express');
var app = express();
var sql = require("mssql");
const bodyParser = require('body-parser');
app.use(bodyParser.json());
var config = {
user: 'user',
password: 'pass',
server: 'localhost',
database: 'Master'
};
app.post('/', function(req, res) {
res.set('Access-Control-Allow-Origin', '*');
const { FirstName, LastName } = req.body;
let connection = new sql.ConnectionPool(config, function(err) {
let request = new sql.Request(connection);
request.query("insert into persons (FirstName, LastName) values ('" + FirstName + "', '" + LastName + "')");
});
res.send({ message: 'Success'})
});
app.listen(5000, () => {console.log('Server is running..')});
And here is my react code...
postData.js:
import React, { Component } from 'react';
class postData extends Component {
constructor() {
super();
this.state = { user: {} };
this.onSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
var self = this;
// On submit of the form, send a POST request with the data to the server.
fetch('http://localhost:5000', {
method: 'POST',
body: {
FirstName: self.refs.FirstName,
LastName: self.refs.LastName
}
})
.then(function(response) {
return response.json()
}).then(function(body) {
console.log(body);
});
}
render() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" placeholder="First Name" ref="FirstName"/>
<input type="text" placeholder="Last Name" ref="LastName"/>
<input type="submit" />
</form>
);
}
}
export default postData;
Again, I'm pretty sure I've screwed up the code on the React side, but I'm pretty new to all this so I'm not sure where to start troubleshooting. When I console.log the req.body on the server side it just returns {}, so it seems like there isn't any data being sent from the react script.
Thanks!
Thanks for all the help guys. I incorporated a lot of the recommendations you guys gave, but the final straw was the following in my server.js code
app.use(express.json({
type: ['application/json', 'text/plain']
}));
Once I included this change I was able to eliminate the undefined error I was getting
Add these lines on fetch request:
fetch('http://localhost:5000',
{ method: 'POST',
headers: new Headers({
"Content-Type":"application/json"
}),
body: JSON.stringify({
FirstName: self.refs.FirstName,
LastName: self.refs.LastName })
})
Then in your server
app.post('/', function(req, res) {
res.set('Access-Control-Allow-Origin', '*');
const body = JSON.parse(req.body);
let connection = new sql.ConnectionPool(config, function(err) {
let request = new sql.Request(connection);
request.query("insert into persons (FirstName, LastName) values ('" + body.FirstName + "', '" + body.LastName + "')");
});
res.send({ message: 'Success'})
});
Hope this helps.
To make your code work just replace this part:
body: {
FirstName: self.refs.FirstName,
LastName: self.refs.LastName
}
by this:
body: {
FirstName: self.refs.FirstName.value,
LastName: self.refs.LastName.value
}
but I would suggest you to do just like #Yousaf did, and use React.createRef(); to create refs to elements in React
POST http://localhost:3000/basicUser/saveUser 500 (Internal Server Error)
Error: Request failed with status code 500
at createError
i get a object with date here , obj : [name: "sourav", phone: "1111111"]
action.js
import { browserHistory } from 'react-router';
import axios from 'axios';
export function saveBasicUser(obj) {
console.log("console log from actions...........")
console.log(obj)
return (dispatch) => {
return axios.post('/basicUser/saveUser', {
headers: { 'Content-Type': 'application/json' },
obj: obj
}).then(function (res) {
console.log(" response from action")
console.log( res)
}).catch(function (err) {
console.log(" err")
console.log( err)
})
}
}
server.js
var basicController = require('./controllers/basicController');
app.post('/basicUser/saveUser', basicController.savebasicUser);
controller file
var dotenv = require('dotenv');
dotenv.load();
var mongoose = require('mongoose');
var BasicUser = require('../models/basicUser');
const fetch = require('node-fetch');
exports.savebasicUser = function(req, res) {
console.log("-------------req from controller-----")
console.log(req)
}
According to this,
you can try to print the error that you are receiving by using console.log(error.response.data); in your action.js