首页 > 解决方案 > 预期声明或声明 (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 新手,所以我知道这可能是微不足道的。谢谢你的帮助!

标签: javascriptnode.jsapi

解决方案


包含 fs.fileRead 函数的 if 语句末尾缺少括号。


推荐阅读