const express = require("express"); const cors = require("cors"); const bodyParser = require("body-parser"); const router = express.Router();

const app = express();

app.use(cors()); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(router); app.use("/",express.static("public")); app.use("/",express.static("src")); app.get("/",(req,res) => { res.sendFile(__dirname + "/" + "index.html"); });

app.listen(3000,() => { console.log("Server running at http://localhost:3000/"); }); 代码没有明显的语法错误。但是需要确保已经安装了express、cors和body-parser模块,以及创建了index.html文件并放置在正确的路径下。

Node.js Express 服务器搭建教程:基础配置与文件服务

原文地址: https://www.cveoy.top/t/topic/qcUk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录