1

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 transaction.commit();
            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.

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 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? Commented Jan 30 at 18:15

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.