,可以通过以下步骤实现:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mysql = require('mysql');
// 创建数据库连接
const connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'password',
database: 'database_name'
});
// 解析请求体
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// 处理删除请求
app.delete('/records', (req, res) => {
const currentDate = new Date();
const changeableDate = new Date('2022-01-01'); // 可更改的日期
if (currentDate > changeableDate) {
// 执行删除操作
const deleteQuery = 'DELETE FROM records';
connection.query(deleteQuery, (error, results) => {
if (error) {
console.error('Error deleting records:', error);
res.status(500).json({ error: 'Failed to delete records' });
} else {
res.json({ message: 'Records deleted successfully' });
}
});
} else {
res.json({ message: 'Deletion not allowed at this time' });
}
});
// 启动服务器
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
请注意,上述代码仅为示例,实际情况中可能需要根据具体需求进行修改和优化。
推荐的腾讯云相关产品:腾讯云数据库MySQL、腾讯云云服务器CVM。
领取专属 10元无门槛券
手把手带您无忧上云