接触nodejs时间不多,使用nodejs写服务端,我现在把所有接口都写在一个文件里面,请问如何将这些接口分开写在不同的文件内,最后将所有的接口文件引入到一个主文件内
我现在所有的接口都类似下面的代码
app.all('/application/message/insert', (req, res) => {
let insertSql = "INSERT INTO application_message SET ?"; //查询用户表的ID
let loginUserInfo = {
"uid": 0
};
let param = req.body;
let selectParams = {
title: param.title,
userID: 0,
content: param.content,
position: param.position,
salary: param.salary,
type: param.type,
dateline: getCurrentDate(2),
workPlace: param.workPlace
};
if (req.session.loginUserInfo !== undefined) {
loginUserInfo = req.session.loginUserInfo;
selectParams["userID"] = loginUserInfo["uid"];
pool.getConnection( (err, connection) => {
if (err) {
throw err;
} else {
connection.query(
insertSql,
selectParams,
(err,data) => {
if(err) {
throw err;
} else {
res.json({
"function": "insert",
"message": "success",
"detail": "求职信息发布"
})
}
}
)
}
pool.releaseConnection(connection);
});
} else {
res.json({
"function": "insert",
"message": "fail",
"detail": "求职信息发布"
})
}
});
/**
* 查询求职信息
*/
app.all("/application/message/select", (req, res) => {
let selectSql = "SELECT id, title, content, dateline, userID, a.position, a.salary, a.type, workPlace, u.name as author, u.sex as sex from application_message a, user_information u where a.userID = u.uid and id=? limit 0,1"; //查询用户表的ID
// let id = req.query.id;
let id = req.body.id;
// console.log("uid ==", uid);
// console.log("开始插入招聘信息");
let returnData = {
code: "",
message: "",
data: ""
};
pool.getConnection( (err, connection) => {
if (err) {
throw err;
} else {
connection.query(
selectSql,
[id],
function (err, rows) {
//console.log(result);
//res.json(result);
if (err) {
throw err;
} else {
// console.log("loginUserInfo ==", loginUserInfo);
returnData["code"] = "0001";
returnData["message"] = "success";
returnData["data"] = rows;
res.json(returnData);
}
// connection.release();
}
)
}
pool.releaseConnection(connection);
})
});