zoukankan      html  css  js  c++  java
  • node.js 配置首页打开页面

    /*var http = require('http');
    var fs = require('fs');
    var url = require('url');

    var http = require("http");
    http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
    }).listen(8888);
    console.log("nodejs start listen 8888 port!");
    */
    var express= require("express");
    var fs = require('fs');
    var documentRoot = 'D:/Program Files/nodejs/www';
    var app=express();
    app.get('/',function(req,res){
    var url = req.url;
    var file=documentRoot+url+"index.html";
    //res.send("Hello word");
    fs.readFile( file , function(err,data){
    /*
    一参为文件路径
    二参为回调函数
    回调函数的一参为读取错误返回的信息,返回空就没有错误
    二参为读取成功返回的文本内容
    */
    if(err){
    res.writeHeader(404,{
    'content-type' : 'text/html;charset="utf-8"'
    });
    res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
    res.end();
    }else{
    res.writeHeader(200,{
    'content-type' : 'text/html;charset="utf-8"'
    });
    res.write(data);//将index.html显示在客户端
    res.end();
    }
    });
    })
    app.listen(8888,function(){
    console.log("现在端口是8888");

    })

     编辑完后 在命令行node 当前js名.js

  • 相关阅读:
    async 和 await
    C#中lock死锁
    Attribute特性
    数据库优化
    EF(ORM)
    依赖注入
    面向接口编程
    EF乐观锁与悲观锁
    为什么要使用RESTFUL风格?
    cloudsim 3.0.3下载与安装教程
  • 原文地址:https://www.cnblogs.com/itadong/p/7151406.html
Copyright © 2011-2022 走看看