zoukankan      html  css  js  c++  java
  • nodejs_100个实例(2)

    nodejs_100个实例(2)

    源码来源:https://www.cnblogs.com/noper/p/6246993.html

    一、本文目标:

    1.使用express进行文件管理,将js代码存放在/public/javasrcipts

    express -e test

    2.从后台读取图片展示在浏览器。图片存放在/public/images

    二、源码(server.js+readImage.js)

    1.server.js
     1 var http = require("http");
     2 var readImage = require("./readImage");
     3 http.createServer(function(res, res){
     4     // res.writeHead(200, {"Content-Type":"text/html; charset=uf-8"});
     5     res.writeHead(200, {"Content-Type":"image/jpeg"});
     6     if (res.url!=="/favicon.ico") {
     7         //res.write('hello,world');//不能向客户端输出任何字节,否则会影响图片的输出
     8         readImage.readImage('../images/dog.jpg', res); //如果文件路径存在则添加数据,如果不存在则新建文件并且添加数据
     9         console.log("继续执行");
    10         //res.end('end'); 在 readImage.readImage方法中已经写过了
    11     }
    12 }).listen(8000);
    13 console.log('Server running at http://127.0.0.1:8000/');
    2.readImage.js
     1 var  fs=  require('fs');
     2 module.exports={
     3     readImage:function(path,res){
     4         fs.readFile(path,'binary',function(err,  file)  {
     5             if  (err)  {
     6                 console.log(err);
     7                 return;
     8             }else{
     9                 console.log("输出文件");
    10                 res.writeHead(200,  {'Content-Type':'image/jpeg'});
    11                 res.write(file,'binary');
    12                 res.end();
    13             }
    14         });
    15     }
    16 };
  • 相关阅读:
    FHS Filesystem Hierarchy Standard(文件系统层次化标准)
    Linux文件类型
    cd、rm、ls命令
    基础内容
    Linux命令格式及使用方法
    Linux一些概念
    SHELL脚本的成分
    SHELL脚本
    Loadrunner关联
    赋值语句
  • 原文地址:https://www.cnblogs.com/carsonwuu/p/8477168.html
Copyright © 2011-2022 走看看