使用nodejs创建返回xml的web server
// Import Node.js core module i.e http
const http = require('http');
const fs = require('fs').promises;
const host = 'localhost';
const port = 8000;
const rssFileName = "/news.rss";
// Create web server
const server = http.createServer(function (req, res) {
// Check the URL of the current request
if (req.url == '/') {
fs.readFile(__dirname + rssFileName)
.then(contents => {
indexFile = contents;
// Set response header
res.writeHead(200, { 'Content-Type': 'text/xml' });
// Set response content
res.write(contents);
res.end();//end the response
})
.catch(err => {
console.error(`Could not read ${rssFileName} file: ${err}`);
process.exit(1);
});
}
else
res.end('Invalid Request!'); //end the response
// Server object listens on port 8081
}).listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
posted on 2024-05-11 22:36 Tencent/Tim 阅读(10) 评论(0) 收藏 举报
浙公网安备 33010602011771号