Skip to content

Instantly share code, notes, and snippets.

@HyunsDev
Last active December 19, 2020 14:24
Show Gist options
  • Save HyunsDev/41d4d1977d01c65d524fe56a1c4c79c6 to your computer and use it in GitHub Desktop.
Save HyunsDev/41d4d1977d01c65d524fe56a1c4c79c6 to your computer and use it in GitHub Desktop.
router.post('/api/write', function (req, res, next) {
let d_line = sanitizeHtml(req.body.line);
let d_date = moment().format('YY.MM.DD h시 mm분');
let d_author = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (d_line && d_date && d_author) {
if (d_line.len > 300) {res.send({status: "err", reason: "ERR_LINE_SO_LONG"});
} else if (d_line.len == 0) {res.send({status: "err", reason: "ERR_LINE_IS_EMPTY"});
} else {
var connection = mysql.createConnection(dbconfig.con);
connection.connect();
connection.query("INSERT INTO dayline.lines (dline, created, author) VALUE (?, ?, ?);", [d_line, d_date, d_author], function (error, results, fields) {
if (error) {
console.log(error);
res.send({
status: "err",
reason: "ERR_UNKNOWN"
})
} else {
res.send({
status: "OK",
data: {
line: d_line,
date: d_date
}
})
}
}
);
connection.end();
}
} else {
res.send({status: "err", reason: "ERR_CHECK_REQUEST"});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment