gpt4 book ai didi

node.js - 使用快速发布方法在数据库中插入新数据

转载 作者:行者123 更新时间:2023-12-03 22:39:27 24 4
gpt4 key购买 nike

我想使用 sequelize express 将数据插入到数据库中,下面的代码工作正常,但我想创建一个文件,如 Controller 、配置、路由、类似的模型和 postman 工具..我尝试了更多,但我没有'得到适当的输出。

const Sequelize = require('sequelize');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb',
parameterLimit: 1000000 }));

const sequelize = new Sequelize('ganeshdb', 'root', 'welcome123$',
{
host: 'localhost',
port: 3306,
dialect: 'mysql'
});
const users = sequelize.define('users', {
id: {
primarykey: true,
type: Sequelize.INTEGER,
},
name: Sequelize.STRING,
role: Sequelize.STRING,
email: Sequelize.STRING
});
app.post('/test', function (request, response) {
return users.create({
id: request.body.id,
name: request.body.name,
role: request.body.role,
email: request.body.email
}).then(function (users) {
if (users) {
response.send(users);
} else {
response.status(400).send('Error in insert new record');
}
});
});

app.listen(3001, function () {
console.log('Express server is listening on port 3000');
});

最佳答案

你可以这样做:

https://expressjs.com/en/starter/generator.html

例子:

npm install express-generator -g
express --view=pug myapp

你可以看到更多 https://solidgeargroup.com/clean-architecture-in-nodejs

关于node.js - 使用快速发布方法在数据库中插入新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com