javascript node.js Server with html -
i have problem node.js server. want host html document there 1 problem typeerror. cann't find mistake. can me?
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 running @ port 3000........");
here log:
"c:\program files (x86)\jetbrains\webstorm 11.0.2\bin\runnerw.exe" "c:\program files\nodejs\node.exe" server.js server running @ port 3000........ _http_outgoing.js:430 throw new typeerror('first argument must string or buffer'); ^ typeerror: first argument must string or buffer @ serverresponse.outgoingmessage.write (_http_outgoing.js:430:11) @ readfilecontext.callback (c:\........\server.js:21:13) @ fsreqwrap.readfileafteropen [as oncomplete] (fs.js:303:13)
later want make mysql connection pooling.
you can use createreadstream
, .pipe
res
@robertklep mentioned, should check if (err)
inside readfile
callback.
here example
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');
update
so match code using express, don't have much:
var express = require('express'); var app = express(); app.use(express.static(__dirname + '/public')); app.listen(3000, function () { console.log('server running @ port 3000........'); });
just put assets in public folder , should go. find more info express can go http://expressjs.com/en/4x/api.html
Comments
Post a Comment