Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有html的javascript node.js服务器_Javascript_Html_Mysql_Node.js - Fatal编程技术网

带有html的javascript node.js服务器

带有html的javascript node.js服务器,javascript,html,mysql,node.js,Javascript,Html,Mysql,Node.js,我的Node.js服务器有问题。我想托管一个html文档,但有一个TypeError问题。我找不到错误。你能帮我吗 var express = require("express"); var mysql = require('mysql'); var app = express(); var fs = require("fs"); var pool = mysql.createPool({ connection

我的Node.js服务器有问题。我想托管一个html文档,但有一个TypeError问题。我找不到错误。你能帮我吗

var express   =    require("express");
var mysql     =    require('mysql');
var app       =    express();
var fs          = require("fs");

var pool      =    mysql.createPool({
    connectionLimit : 100, //important
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'test',
    debug    :  false
});

app.get('/', function(req, res) {
    fs.readFile('index.html', 'utf-8',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        res.end();
    });
});

app.listen(3000);
console.log("SERVER IS NOW RUNNING AT PORT 3000........");
下面是日志:

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" Server.js
SERVER IS NOW RUNNING AT PORT 3000........
_http_outgoing.js:430
    throw new TypeError('first argument must be a string or Buffer');
    ^

TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:430:11)
    at ReadFileContext.callback (c:\........\Server.js:21:13)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:303:13)

稍后我想用池建立一个MySQL连接。

您也可以使用
createReadStream
.pipe
res
,但是正如@robertklep所提到的,您应该在
readFile
回调中检查
如果(错误)

以下是示例

var fs = require('fs');
var http = require('http');

var file = fs.createReadStream('/path/to/file');

var server = http.createServer(function (req, res) {
    // res.writeHead(200, {
    //     'content-type': 'text/plain'
    // });

    file.pipe(res);
});

server.listen('3000');
更新

因此,要使用express匹配您的代码,您不需要做很多工作:

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

app.listen(3000, function () {
    console.log('SERVER IS NOW RUNNING AT PORT 3000........');
});

只要把你所有的资产都放在公用文件夹里,你就可以走了。要查找有关Express的更多信息,您可以转到server.js文件第21行的

?您没有检查是否设置了
err
,它可能是…'res.write(data);'这是第21行。你能给我举个例子吗?@JonasWolff我用这个例子更新了答案。希望能有帮助:谢谢你,但我现在有一个新问题。css和jQuery移动资源不在网站的源代码中。@JonasWolff我使用express添加了另一个示例。我添加了允许跨源代码,因为我有一些时间压力。我会检查这个,当项目准备好