【NodeJS】尝试【一】

    技术2022-05-20  29

    尝试内容:读文件、访问计数、取本机时间、取本机IP地址、取本机进程列表 尝试成功:读文件、访问计数、取本机时间 尝试失败:取本机IP地址、取本机进程列表 读取文件:

    #! /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()); } 

    最新回复(0)