使用 fs.writeFile 写入文本文件后停止执行
Stop execution after writing to text file with fs.writeFile
我有以下 Node.JS(运行 使用 Express)代码:
let app = express();
app.use(cors());
app.get('/callback', function (req, res) {
// your application requests refresh and access tokens
// after checking the state parameter
var code = req.query.code || null;
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(clientId + ':' + clientSecret).toString('base64'))
},
json: true
};
request.post(authOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token,
refresh_token = body.refresh_token;
fs.writeFile('test.txt', 'HELLO', function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});
}
}
)
});
console.log('Listening on 8888');
app.listen(8888);
该路由用作对 Spotify Web 请求的回调 API,因此我可以获得访问令牌。
Spotify 然后重定向到上面的回调函数,您可以通过查看“redirect_uri”在 URI 中看到它。
如果您需要有关 Spotify 授权流程的更多信息,请参阅 here。
这是我用来向 Spotify 验证我的应用程序的 URI。
CLIENT_ID在我的请求中被替换为我真正的CLIENT_ID
我的问题在文件写入部分:
fs.writeFile('test.txt', 'HELLO', function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});
当 Spotify 调用回调路由时,我的文本文件中写入了字符串“HELLO”,因此文件写入功能正常。
但即使它已完成写入字符串,Chrome 页面在服务器上仍然是 运行 和“待处理”。它运行了几分钟,然后因为页面未发送任何数据而崩溃。为什么?
我看过 this page 谈论写入文本文件的方法,使用 writeFile 和 writeFileAsync,但是使用它们都没有解决我的问题。
编辑: 我真的不想停止 Express 进程!我只想能够处理另一个请求:)
有什么想法吗?提前致谢:)
您没有从路线返回任何东西,请尝试添加 res.send({})
在您的获取路径中您没有发送响应,无论写入文件是否成功,您都必须发送响应。
添加以下代码post写入文件(以及在错误情况下)
res.send({YOUR_CHOICE_RESPONSE_DATA})
我有以下 Node.JS(运行 使用 Express)代码:
let app = express();
app.use(cors());
app.get('/callback', function (req, res) {
// your application requests refresh and access tokens
// after checking the state parameter
var code = req.query.code || null;
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(clientId + ':' + clientSecret).toString('base64'))
},
json: true
};
request.post(authOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token,
refresh_token = body.refresh_token;
fs.writeFile('test.txt', 'HELLO', function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});
}
}
)
});
console.log('Listening on 8888');
app.listen(8888);
该路由用作对 Spotify Web 请求的回调 API,因此我可以获得访问令牌。
Spotify 然后重定向到上面的回调函数,您可以通过查看“redirect_uri”在 URI 中看到它。
如果您需要有关 Spotify 授权流程的更多信息,请参阅 here。
这是我用来向 Spotify 验证我的应用程序的 URI。
CLIENT_ID在我的请求中被替换为我真正的CLIENT_ID
我的问题在文件写入部分:
fs.writeFile('test.txt', 'HELLO', function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});
当 Spotify 调用回调路由时,我的文本文件中写入了字符串“HELLO”,因此文件写入功能正常。
但即使它已完成写入字符串,Chrome 页面在服务器上仍然是 运行 和“待处理”。它运行了几分钟,然后因为页面未发送任何数据而崩溃。为什么?
我看过 this page 谈论写入文本文件的方法,使用 writeFile 和 writeFileAsync,但是使用它们都没有解决我的问题。
编辑: 我真的不想停止 Express 进程!我只想能够处理另一个请求:)
有什么想法吗?提前致谢:)
您没有从路线返回任何东西,请尝试添加 res.send({})
在您的获取路径中您没有发送响应,无论写入文件是否成功,您都必须发送响应。
添加以下代码post写入文件(以及在错误情况下)
res.send({YOUR_CHOICE_RESPONSE_DATA})