右匀 2019-03-25 09:52 采纳率: 0%
浏览 1785

在node.js环境下,引用<script>报错, Uncaught SyntaxError: Unexpected token <求解!!!!!

起服务的代码:

var http = require('http');
var fs = require('fs');
var path = require('path')
var mime =require('mime');
var cache = {};


//所请求的文件不存在时发送404错误
function send404(response){
    response.writeHead(404,{'Conten-Type': 'text/plain'});
    response.write('Error 404 : resource not found');
    response.end();
}  


//提供文件数据服务
function sendFile(response,filePath,fileContents){
    response.writeHead(
        200,
        {
            'Content-Type':mime.getType(path.basename(filePath))
        }
    );
    response.end(fileContents);
}


//判断文件是否被缓存
function serverStatic(response,cache,absPath){
    console.log(cache)
    if(cache[absPath]){
        console.log(cache[absPath]);
        sendFile(response,absPath,cache[absPath]);//从内存中返回文件
    }else{
        fs.exists(absPath,function(exists){
            if(exists){
                fs.readFile(absPath,function(err,data){//从硬盘中读取文件
                    if(err){
                        send404(response);
                    }else{
                        cache[absPath] = data;
                        sendFile(response,absPath,data);//从硬盘中读取文件并返回
                    }
                })
            }else{
                send404(response);
            }
        });
    }
}


var server = http.createServer(function(req,res){
    var filePath = false;
    var webPage = fs.readFileSync('../html/webpage.html');//同步读取

    if(req.url == '/'||req.url=="/favicon.ico"){
        filePath = '../js/webpage.js'
    }else{
        filePath = '../'+req.url;
    }
    var absPath = filePath;//./static/js/webpage.js
    console.log(absPath);
    res.write(webPage);
    serverStatic(res,cache,absPath);
    res.end();
})


server.listen('3000',function(err){
    if(err){
        console.log(err);
        throw err;
    }
    console.log('服务器已开启');
})

渲染页面的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" type="Content-type:application/x-javascript" href="../css/webpage.css">
</head>
<body>
    <div class="clearfix wrapper">
        <p class="storeTitle">商城</p>
    </div>
    <script src="../js/webpage.js" type="text/javascript"></script>
</body>
</html>

引用的webpage.js代码:

var screenheight =window.innerHeight;
var wrapper = $('.wrapper');

报错信息:

图片说明

  • 写回答

2条回答 默认 最新

  • 右匀 2019-03-25 10:35
    关注

    去掉req.write和req.end就不会write after end

    var server = http.createServer(function(req,res){
        var filePath = false;
        // var webPage = fs.readFileSync('../html/webpage.html');//同步读取
        if(req.url == '/'||req.url=="/favicon.ico"){
            filePath = '../html/webpage.html'
        }else{
            filePath = '..'+req.url;
        }
        var absPath = filePath;//./static/js/webpage.js
        console.log(req.url);
        serverStatic(res,cache,absPath);
    })
    

    静态文件服务器没有问题了,那个报错就没有了

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?