Node.js学习笔记4-模块、POST请求(文件上传)

2019-06-11  本文已影响0人  页川叶川

1.模块

1.1.示例:time-modules模块

使用

//test2:npm引入模块使用

//需要使用一个日期
const dateTime = require('date-time');

var time = dateTime();

console.log(time);

//npm init 会将项目信息打包成一个json文件,其中dependencies中包含的是项目的所有依赖
//npm install会安装该项目中所有的依赖

1.2.示例:self-modules模块

1.2.1.创建People类

function  People(name,sex,age) {
    this.name = name;
    this.sex = sex;
    this.age = age;
}

People.prototype = {
    sayHello : function(){
        console.log(this.name+this.sex+this.age);
    }
}

//此时,People被视为构造函数,可以用new来实例化
module.exports = People;

1.2.2.创建属性与函数文件

var msg = "你好";
function show() {
    console.log("啦啦啦");
}

exports.msg = msg;
exports.show = show;

1.2.3.测试:test

test1文件夹:module模块入门

对应test.js

// test.js
var test = require("./node_modules/test");

console.log(test.msg);
test.show();

对应People.js

var People = require("./node_modules/People");

var xiaoming = new People("小明","nan","12");

xiaoming.sayHello();

1.3.示例:ejs-moban

1.3.1.ejs测试

var ejs = require("ejs");

//模板
var string = "好高兴啊,今天买了华为mate<%= a %>"
//数据
var data = {
    a:20
};

//数据绑定
var html = ejs.render(string,data);
//输出
console.log(html);

1.3.2.使用ejs

index.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>好高兴啊,今天买了华为mate<%= a %></h1>
    <ul>
        <% for (var i=0; i<news.length; i++){ %>
            <li><%= news[i] %></li>
        <% } %>
    </ul>
</body>

</html>

数据绑定文件:demo.js

var ejs = require("ejs");
var fs = require("fs");
var http = require("http")

var server = http.createServer(function (req,res) {
    fs.readFile("./views/index.ejs",function (err,data) {
        //绑定模板
        var template = data.toString();
        var dictionary = {
            a:20,
            news:["1班太牛逼啦","2班太牛逼啦","3班太牛逼啦","4班太牛逼啦","5班太牛逼啦","6班太牛逼啦"]
        };
        var html = ejs.render(template,dictionary);

        //显示
        res.writeHead(200,{"Content-Type":"text/html;charset=UTF8"});
        res.end(html)
    });
});

server.listen(3000,"127.0.0.1");

2.字符串处理

var http = require("http");
var querystring = require("querystring");


var server = http.createServer(function (req,res) {
    if(req.url == "/dopost" && req.method.toLowerCase() == "post"){
        var alldata = "";
        req.addListener("data",function (chunk) {
            alldata += chunk;
        });

        req.addListener("end",function () {
            var datastring = alldata.toString();
            var dataObj = querystring.parse(datastring);
            console.log(dataObj.name);
            console.log(dataObj.sex);
            console.log(dataObj.hobby)
            res.end();
        });
    }
});

server.listen(3000,"127.0.0.1");

3.文件上传

var http = require("http");
var querystring = require("querystring");
//引入工具类
var formidable = require('formidable');
var util= require("util");
var fs = require("fs");
var dateTime = require("date-time");
var path = require("path");


var server = http.createServer(function (req,res) {
    if(req.url == "/dopost" && req.method.toLowerCase() == "post"){

        // parse a file upload
        var form = new formidable.IncomingForm();
        //设置文件上传地址
        form.uploadDir = "./uploads";

        form.parse(req, function(err, fields, files) {
            if(err){
                throw err;
            }
            // console.log(fields);
            // console.log(files);
            //所有的文本域,单选框,都在fields存放
            //所有的文件,files

            //输出展开后的文件信息
            console.log(util.inspect({fields:fields,files:files}));

            //当前时间
            var time = dateTime();   //date-time模块函数          //目前这个时间有毒,有非法字符
            var ran = parseInt(Math.random() * 89999 + 10000);   //5位随机数
            var extname = path.extname(files.tupian.name);       //取文件后缀名

            //执行改名
            var oldpath = __dirname + "/" + files.tupian.path;
            //新的路径由三部分组成:时间戳,随机数,拓展名
            var newpath = __dirname + "/uploads/" + ran +extname;
            console.log(newpath);
            //改名
            fs.rename(oldpath,newpath,function (err) {
                if (err){
                    throw Error("改名失败");
                }
                res.writeHead(200, {'content-type': 'text/plain'});
                res.end("成功");
            });

        });

    }
});

server.listen(3000,"127.0.0.1");

4.初识express


var express  = require("express");

var bodyParser = require("body-parser");

var app = express();

//模板引擎
app.set("view engine","ejs");

app.get("/",function (req,res) {
    res.render("form");
})

app.use(bodyParser.urlencoded({ extended: false }));

app.post("/",function (req,res) {
    console.log(req.body);
})

app.listen(3000);

文集推荐:

Java基础方法集1
Python基础知识完整版
Spring Boot学习笔记
Linux指令进阶
Java高并发编程
SpringMVC基础知识进阶
Mysql基础知识完整版
健康管理系统学习花絮(学习记录)
Node.js基础知识(随手笔记)
MongoDB基础知识
Dubbo学习笔记
Vue学习笔记(随手笔记)

声明:发表此文是出于传递更多信息之目的。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与本我们(QQ:981086665;邮箱:981086665@qq.com)联系联系,我们将及时更正、删除,谢谢。

上一篇下一篇

猜你喜欢

热点阅读