预期的声明或声明(JavaScript)

时间:2020-08-15 03:48:26

标签: javascript node.js api

我目前遇到一个问题,我在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函数之后才发生的。如果我提出逻辑,这里的东西就会出错。我是Java语言的新手,所以我知道这可能很简单。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

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();
        })
      } // you're missing this curly brace, it closes the else if above
        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')

答案 1 :(得分:0)

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

相关问题