美文网首页
node中的模块系统

node中的模块系统

作者: SuperHong521 | 来源:发表于2018-12-17 09:15 被阅读0次

1.http:搭建后台服务器

1.引入http模板
const http = require('http');
2.搭建服务
var server = http.createServer(function(request,response){
  res.write();//响应的内容
  res.end();//响应结束
  req.url();//请求的路径
})
3.指定端口号
server.listen(8080)

fs 文件操作模块

读取文件
const fs = require('fs');
fs.readFile(文件名,funtion(err,data){//err错误 data数据
  if(err){
    console.log('错误')
  }else{
    console.log(data.toString())//转化为字符串
  }
})
写文件
var fs = require('fs');
fs.writeFile('文件名','内容',function(err){
  console.log(err)//如果没有报错报null;若有错则报错
})

fs模板结合http模板请求不同文件

const http = require('http');
const fs = require('fs');
var server = http.createServer(function(req,res){
  var file_name = './www'+req.url;
  fs.readFile(file_name,function(err,data){
    if(err){
        console.log(err)
    }else{
        res.write(data)
    }
    res.end()
  })
})
server.listen(8080);

相关文章

  • Node.js教程(02)|基础知识篇(下)

    Node.js 模块系统 模块系统,便于各个文件之间的相互调用。在node.js中,模块就是文件,文件也是模块,这...

  • 无标题文章

    node模块与包管理 在Node中,使用的是Commonjs模块标准,commonjs模块系统是文件之间共享对象或...

  • node中的核心模块

    node的模块有很多,但常用的只有几个核心模块,例如fs文件系统模块,path模块,http模块等。node中的J...

  • 01-Node 基础使用

    Node 基础使用Node 介绍Node 模块化开发模块成员的导出模块成员的导入Node 系统模块 path 和 ...

  • Nodejs的模块引入与回调函数

    Node.js模块系统 在 Node.js 中,引入一个模块 如: var http=require('fs');...

  • Node 中的模块系统

    使用 Node 编写应用程序主要是使用 EcmaScript 语言(和游览器中不一样,在 Node 中没有 BOM...

  • node中的模块系统

    1.http:搭建后台服务器 fs 文件操作模块 读取文件 写文件 fs模板结合http模板请求不同文件

  • 4_Node模块系统

    [TOC] Node.js模块系统 为了让Node的文件可以相互调用,Node提供了一个简单的模块系统模块是Nod...

  • Node学习(6)--模块系统

    Node.js模块系统 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统。模块是No...

  • node.js(十一)

    Node.js模块系统为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统。模块是Nod...

网友评论

      本文标题:node中的模块系统

      本文链接:https://www.haomeiwen.com/subject/vqxqkqtx.html