You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js使用Express启动项目时script.js加载报ERR_ABORTED 404如何解决

问题原因

你当前的Express服务仅配置了根路径/的路由返回index.html文件,没有开启静态资源托管能力,浏览器发起script.js等静态资源请求时,服务端无法匹配到对应路由,因此返回404错误。

解决方法
  • 第一步:在app.js中添加Express内置的静态资源托管中间件,配置要放在路由定义之前,修改后的完整app.js代码如下:
const express = require('express')
const port = 3000;
const path = require('path')

const app = express()
// 新增静态资源托管配置,__dirname代表当前app.js所在目录
app.use(express.static(__dirname))

app.get('/', function(request, response){
    response.sendFile(path.join(__dirname, '/index.html'));
})

app.listen(port)
  • 第二步(可选优化):检查index.html中的样式引入标签,补全缺失的rel="stylesheet"属性,避免部分浏览器无法正常识别样式资源:
<link rel="stylesheet" href="styles/styles.css">

修改完成后重启Express服务,刷新页面即可正常加载script.js。

内容的提问来源于stack exchange,提问作者AndreGTH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 07:24:03