#! /usr/local/bin/node var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { showHomePage(req, res); }).listen(8080, "172.16.131.68"); console.log('Server running at http://172.16.131.68:8080/'); function showHomePage(req, res) { //设置头信息 res.writeHead(200, {'Content-Type': 'text/html'}); //读取首页 fs.readFile('/home/oicq/js_web/htdocs/index.html', function (err, data) { if (err) { res.end('server encounter err/n'); } else { res.end(data); } }); }
访问计数#! /usr/local/bin/node var http = require('http'); var fs = require('fs'); var pv = 0; http.createServer(function (req, res) { showCounter(req, res); }).listen(8080, "172.16.131.68"); console.log('Server running at http://172.16.131.68:8080/'); function showCounter(req, res) { //设置头信息 res.writeHead(200, {'Content-Type': 'text/html'}); //输出访问次数 res.end('you are the ' + ++pv + ' vistor'); }
取时间#! /usr/local/bin/node var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { showServerTime(req, res); }).listen(8080, "172.16.131.68"); console.log('Server running at http://172.16.131.68:8080/'); function showServerTime(req, res) { //设置头信息 res.writeHead(200, {'Content-Type': 'text/html'}); //输出访问次数 var date = new Date(); res.end('current time is ' + date.getYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()); }