javascript - 预期声明或声明 (Javascript)
问题描述
我目前遇到一个问题,我在我的 Javascript 服务器中添加了一个当前引发错误的函数。
var http = require('http');
var fs = require('fs')
var server = http.createServer(function (req, res){
//handle incoming requests here
if (req.url == '/') {
res.writeHead(200, { 'Content-Type': 'text/html'}); //sets the headers for the response
res.write('Welcome to project perfect'); //write an about me page
res.end();
}
else if (req.url == '/spotify' ) {
res.writeHead(200, { 'Content-Type': 'text/html'});
fs.readFile('./index.html', function (err, data) { //Figure out why the if logic is making line 39 error
res.write(data);
res.end();
});
##错误在 else if 语句中被抛出。
else if (req.url == '/fitness') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("Welcome to aroe's fitness page"); //integrate myfitnesspal api and workout recorder
res.end();
}
else if (req.url == '/blog') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('blog');
res.end();
}
})
server.listen(8000);
console.log('SERVING ON PORT 8000')
错误是说
declaration or statement expected at the this chunk `else if (req.url == '/fitness') {`
它只发生在我添加 fs.fileread 函数之后。如果我提出逻辑,则下一个错误会出错。我是 Javascript 新手,所以我知道这可能是微不足道的。谢谢你的帮助!
解决方案
包含 fs.fileRead 函数的 if 语句末尾缺少括号。
推荐阅读
- django - 如何使用 SQS 启动 Celery 容器
- android - 选择图像并将结果发送到android studio中的另一个活动保持强制关闭
- wordpress - 单个 URL 的 Worpress htaccess 重写规则
- mongodb - 如何使用猫鼬$near?(类型数据位置)
- postgresql - 码头工人 | Postgres 数据库未初始化且未指定超级用户密码
- python - 保存启用宏的电子表格时出现 pywin/win32com 文件访问错误
- java - Spring Security - REST API 的授权
- python - Python:如果使用正则表达式从另一个字符串中提取子字符串
- php - Wordpress+ACF - 通过转发器字段中的多选选项进行过滤
- search-engine - 将 TF-IDF 值存储在倒排索引中