首页 > 解决方案 > 如何为我可以使用 node.js 在一个文件中更改的网站创建标题

问题描述

我正在尝试从视频中为我正在处理的网站创建一个 node.js 标头,但是无论视频声称多么简单,我都无法弄清楚我做错了什么

const http = require('http'); 
const fs = require("fs");
const port = 3000;

const server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' })
    fs.readFile('Header.html', function(error, data) {
        if(error) {
            res.writeHead(404)
            res.write('Error: Header not found')
        } else {
            res.write(data)
        }
        res.end()
    })
}) 

server.listen(port, function(error) {
    if(error) {
        console.log("Something went wrong: " + error);
    } else {
        console.log("Server is up and running on port " + port);
    }
})

标签: node.js

解决方案


推荐阅读