通过React Form中的节点进行API连接

时间:2019-04-23 10:31:14

标签: node.js reactjs api

Can't able to solve this error, tried with npm i but it's an error of file not found      const express = require('express');     const mysql = require('mysql');     const app = express();     const bodyParser = require('body-parser');     var cors = require('cors');

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());



function getConnection() {
    return mysql.createConnection({
        host: 'localhost',
        user: 'root',
        database: 'pms_tool'
    })
}



//DEL REQUEST
app.delete('/users/:kpiId', (req, res) => {
    console.log('Fetching user with kpiId: ' + req.params.kpiId);

    const connection = getConnection();

    const Id = req.params.kpiId;

    const queryString = 'DELETE FROM kpi_master WHERE kpiId = ?';
    connection.query(queryString, [Id], (err, rows, fields) => {
        if (err) {
            console.log('Failed to query for users: ' + err);
            res.sendStatus(500);
            return;
        }
        res.end('Record has been deleted!!!');
    });
});


//update kpi api 
app.put("/kpi_update/:kpiId", (req, res) => {
    const id = req.params.kpiId;
    const name = req.body.kpiName;
    const description = req.body.description;
    const queryString = " UPDATE kpi_master SET kpiName =  ? , description = ? WHERE kpiId = ? "
    getConnection().query(queryString, [name, description, id], (err, results, fields, rows) => {
        if (err) {
            console.log("Not updated " + err);
            res.sendStatus(500);
            return
        }
        console.log('record updates ' + results.id)
        res.send(results)
    })

})


//create a new kpi
app.post('/user_create', (req, res) => {
    console.log('Trying to create a new user...');
    console.log('first name: ' + req.body.kpiName);
    const kpiName = req.body.kpiName;
    const description = req.body.description;
    const queryString = 'INSERT INTO kpi_master (kpiName,description) values(?,?)';
    getConnection().query(queryString, [kpiName, description], (err, results, fields) => {
        if (err) {
            console.log('Failed to insert new user :' + err);
            res.sendStatus(500);

            return;
        }
        console.log('Inserted a new user with id:', results.insertId);

        res.end();
    });
    res.end();
});




app.get('/', (req, res) => {
    console.log('Responding to root route');
    res.send('Hello from ROOT');
});


//get kpi by single ID
app.get('/users/:kpiId', (req, res) => {
    console.log('Fetching user with kpiId: ' + req.params.kpiId);

    const connection = getConnection();

    const userId = req.params.kpiId;

    const queryString = 'SELECT * FROM kpi_master WHERE kpiId = ?';
    connection.query(queryString, [userId], (err, rows, fields) => {
        if (err) {
            console.log('Failed to query for users: ' + err);
            res.sendStatus(500);
            return;
        }

        console.log('I think we fetched users successfully');

        const users = rows.map((row) => {
            return { kpiName: row.kpiName, description: row.description };
        });

        res.json(users);
    });

    // res.end()
});


//get kpi
app.get('/users', (req, res) => {
    const connection = getConnection();
    const queryString = 'SELECT * FROM kpi_master';
    connection.query(queryString, (err, rows, fields) => {
        if (err) {
            console.log('Failed to query for users: ' + err);
            res.sendStatus(500);
            return;
        }
        res.json(rows);
    });
});


const port = 5000;

app.listen(port, () => `Server running on port ${port}`);

共享图片后出现错误

After installing mysql package i got this error and now the form is also not working program runs succesfully but it'snot showing the data, as it is working in postman我已经制作了一个用于列出记录的数据表,并且我们可以通过api添加它。因此,现在我已经制作了api并添加了一些记录,并为api创建了一个名为server的文件,该文件可以正常工作,但是我以react形式调用了api,但无法列出该api。它显示了此错误... enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您的node应用程序中未安装用于mysql的node_module。请使用

安装
npm i mysql

然后重新启动您的应用程序

谢谢。