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 |1 Answer
Reset to default 1Your 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
版权声明:本文标题:javascript - app.post inserting data error: Cannot Get using NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741947565a2406512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 visithttp://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