当我运行curl脚本并且我的express服务器代码在systemd之外运行时,会在我的数据库表中创建一条记录,并将一个json“用户”对象返回到stdout。当我的 Express 服务器代码在 systemd 中运行时,curl 脚本会在我的数据库表中创建一条记录。但是,“用户”对象不会返回到标准输出,并且curl 脚本似乎挂起。我必须按 ctrl-c 来终止它。当涉及 systemd 时,为什么我在 stdout 中看不到用户对象?
这是我的卷曲脚本。
curl -v -X POST -H "Content-Type: application/json" -d '{"userName":"bill", "password":"your_password", "email" : "[email protected]"}' http://some-server:8080/api/users/signup
这是我的服务器代码。在响应中发送用户对象的代码是(见下文):
return res.status(201).send(user);
const bcrypt = require("bcrypt");
const db = require("./index.js");
const jwt = require("jsonwebtoken");
const User = db.users;
// Signing a user up. Hash user password before
// its saved to the database with bcrypt.
const signup = async (req, res) => {
try {
const { userName, email, password } = req.body;
const data = {
userName,
email,
password: await bcrypt.hash(password, 10),
};
// Save user in the database
const user = await User.create(data);
// If a user's details are captured generate a token
// with the user's id and the SECRET_KEY in the env
// file set cookie with the token generated
if (user) {
let token = jwt.sign({ id: user.id }, process.env.SECRET_KEY, {
expiresIn: 1 * 24 * 60 * 60 * 1000,
});
// Send user's details
return res.status(201).send(user);
} else {
return res.status(409).send({"error" : "Your account could not be created with the information you supplied."});
}
} catch (error) {
console.log(error);
}
};
module.exports = {
signup
};
解决了这个问题。我将 process.env.SECRET_KEY 存储在 systemd 无法读取的 .env 文件中。通过将密钥添加到 systemd 单元文件来修复。