剧场模式
首页后端NodejsNodejs + Express + MongoDB 基础篇

Node.js + Express + MongoDB 基础篇 #8 使用模板引擎

海外散仙厉飞雨 · 真仙发布于
1

server.js

var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');

var app = express();

app.set('view engine', 'ejs');

var multer = require('multer');

var createFolder = function(folder) {
    try {
        fs.accessSync(folder);
    } catch (e) {
        fs.mkdirSync(folder);
    }
};

var uploadFolder = './upload/';

createFolder(uploadFolder);

var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, uploadFolder);
    },
    filename: function(req, file, cb) {
        cb(null, file.originalname);
    }
});

var upload = multer({ storage: storage });

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.get('/', function(req, res) {
    console.dir(req.query);
    res.send("home page: " + req.query.find);
});

app.get('/form/:name', function(req, res) {
    var data = { age: 29, job: "programmer", hobbie: ['eating', 'fighting', 'fishing'] };
    res.render('form', { data: data });
});

app.get('/about', function(req, res) {
    res.render('about');
});

app.post('/', urlencodedParser, function(req, res) {
    console.dir(req.body);
    res.send(req.body.name);
});

app.post('/upload', upload.single('logo'), function(req, res) {
    console.dir(req.file);
    res.send({ 'ret_code': 0 });
});

app.get('/profile/:id/user/:name', function(req, res) {
    console.dir(req.params);
    res.send("You requested to see a profile with the name of " + req.params.name);
});

app.get('/ab?cd', function(req, res) {
    res.send('/ab?cd');
})

app.listen(3000);
console.log('listening to port 3000');

form.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>
    <%- include('partials/header.ejs') -%>
        <h1>
            <%= data.age %>
                <h2>hobbie</h2>
                <ul>
                    <% data.hobbie.forEach(function(item) { %>
                        <li>
                            <%= item %>
                        </li>
                        <% }) %>
                </ul>
        </h1>
        <form action="/upload" method="post" enctype="multipart/form-data">
            <h2>单图上传</h2>
            <input type="file" name="logo">
            <input type="submit" value="提交">
        </form>
</body>

</html>

about.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>
    <%- include('partials/header.ejs') -%>
        <p>about page</p>
</body>

</html>

header.ejs

<nav>
    <ul>
        <li><a href="">home</a></li>
        <li><a href="">about</a></li>
    </ul>
</nav>
0 条回复
暂无回复~~
学员
dechuangchang · 金仙后期不乱方寸 · 真仙kelly · 大罗大梦 · 真仙董博文 · 太乙玉仙VessieWoo · 真仙liar · 真仙slinloving · 真仙薄情人 · 道祖mtt3366 · 金仙后期ChenWes · 金仙中期itlaowang520 · 太乙巅峰iosLongFeng · 太乙巅峰andyliangshan · 真仙liaokui · 真仙JameKing2018 · 真仙qianduan · 真仙496087321 · 真仙koiomi · 真仙madecc · 真仙zbx7858 · 道祖leo · 太乙玉仙q888204573 · 金仙中期Shen6s · 真仙
最新动态
dechuangchang · 金仙后期学习到了6:21
不乱方寸 · 真仙学习到了6:20
kelly · 大罗学习到了6:18
大梦 · 真仙学习到了6:26
董博文 · 太乙玉仙学习到了6:27

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:31

Top