admin管理员组

文章数量:1313347

I am trying to insert data into the SQL Server database using NodeJS.

When I ran the service and opened the browser-> localhost:5000 and I

got the page:

Error: Cannot Get (404) in NodeJS.

Seeking help if I am missing something. The database table has only 2 columns

Here's my code. Hoping for some assistance, will be highly appreciated

    const express = require('express');
    const sql = require('mssql');
    const cors = require('cors');
    const app = express();
    app.use(cors());
    app.use(express.json());

    const config = {
    user: 'sa',
    password: 'password',
    server: 'ServerName\\MSSQL2019EX',
    database: 'Contacts',
    options: {
        encrypt: false,
        trustServerCertificate: false
      }
    };

    app.post('/', async (req, res) => {
    try {
        await sql.connect(config);
        const pool = new sql.ConnectionPool(config);
        await pool.connect();

        const transaction = new sql.Transaction(pool);
        await transaction.begin();

        try {
            for (const user of req.body) {
                await new sql.Request(transaction)
                    .input('name', sql.NVarChar, user.name)
                    .input('email', sql.NVarChar, user.email)
                    .query(`
                        INSERT INTO Users (Name, Email) 
                        VALUES (@name, @email)
                    `);
            }
            await transactionmit();
            res.sendStatus(200);
        } catch (err) {
            await transaction.rollback();
            throw err;
        }
    } catch (error) {
        console.error('SQL error:', error);
        res.status(500).send('Database error');
    }
});

app.listen(5000, () => {
    console.log('Server running on port 5000');
});

Asking for suggestion of fixing the script.

I am trying to insert data into the SQL Server database using NodeJS.

When I ran the service and opened the browser-> localhost:5000 and I

got the page:

Error: Cannot Get (404) in NodeJS.

Seeking help if I am missing something. The database table has only 2 columns

Here's my code. Hoping for some assistance, will be highly appreciated

    const express = require('express');
    const sql = require('mssql');
    const cors = require('cors');
    const app = express();
    app.use(cors());
    app.use(express.json());

    const config = {
    user: 'sa',
    password: 'password',
    server: 'ServerName\\MSSQL2019EX',
    database: 'Contacts',
    options: {
        encrypt: false,
        trustServerCertificate: false
      }
    };

    app.post('/', async (req, res) => {
    try {
        await sql.connect(config);
        const pool = new sql.ConnectionPool(config);
        await pool.connect();

        const transaction = new sql.Transaction(pool);
        await transaction.begin();

        try {
            for (const user of req.body) {
                await new sql.Request(transaction)
                    .input('name', sql.NVarChar, user.name)
                    .input('email', sql.NVarChar, user.email)
                    .query(`
                        INSERT INTO Users (Name, Email) 
                        VALUES (@name, @email)
                    `);
            }
            await transactionmit();
            res.sendStatus(200);
        } catch (err) {
            await transaction.rollback();
            throw err;
        }
    } catch (error) {
        console.error('SQL error:', error);
        res.status(500).send('Database error');
    }
});

app.listen(5000, () => {
    console.log('Server running on port 5000');
});

Asking for suggestion of fixing the script.

Share Improve this question edited Jan 30 at 17:30 ADyson 62.1k16 gold badges78 silver badges91 bronze badges asked Jan 30 at 17:29 Jake YambaoJake Yambao 111 silver badge1 bronze badge 2
  • 3 The code you've shown us allows for a POST request to be made for it (in the app.post code), and can handle that. There's nothing in what you've shown us which will handle GET requests. But if you simply visit http://localhost:5000 in your browser, your browser will always send a GET request to the app server. I think nodeJS is telling you it doesn't have a route which can handle your GET request, which the content of your code seems to agree with. Perhaps you would be better to test your endpoint with a tool such as PostMan instead, ensuring you set it to send a POST and supply parameters – ADyson Commented Jan 30 at 17:29
  • 2 What do you expect to be in the request body when you simply open the URL in the browser? – Barmar Commented Jan 30 at 18:15
Add a comment  | 

1 Answer 1

Reset to default 1

Your app only defines a POST route. When you open the browser and navigate to it, it sends a GET request.

You could either add a GET route with app.get, or use a POST request instead of a GET request (e.g., with curl, Postman, or any other tool of your choice).

本文标签: javascriptapppost inserting data error Cannot Get using NodeJSStack Overflow