Node.js express server render HTML template All In One
Node.js express server render HTML template All In One
HTML 模版引擎
Template Engine
https://expressjs.com/en/guide/using-template-engines.html
https://expressjs.com/en/advanced/developing-template-engines.html
jade
/pug
/ejs
# old version, jade
# new version, pug
$ npm i pug
# OR
$ yarn add pug
https://pugjs.org/api/getting-started.html
$ npm i ejs
$ npm i jade
https://www.npmjs.com/package/pug
https://www.npmjs.com/package/jade
https://www.npmjs.com/package/ejs
https://www.npmjs.com/package/mustache
demos
/**
* express static server for react build/dist test!
*/
// simple express server for HTML pages!
// ES6 style
const express = require('express');
// const bodyParser = require('body-parser');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const app = express();
// Starting with release 4.16.0, a new express.json() middleware is available.
app.use(express.json());
app.use(express.urlencoded({
extended: true,
}));
app.use(express.static(__dirname + '/public'));
// 模版引擎 views
app.set('views', './views');
app.set('view engine', 'pug');
// parse application/json
// app.use(bodyParser.json());
// parse application/x-www-form-urlencoded, multipart/form-data
// app.use(bodyParser.urlencoded({ extended: true }));
// let cache = [];// Array is OK!
// cache[0] = fs.readFileSync( __dirname + '/index.html');
// cache[1] = fs.readFileSync( __dirname + '/views/testview.html');
// app.get('/', (req, res) => {
// res.setHeader('Content-Type', 'text/html');
// res.send( cache[0] );
// });
// app.get('/test', (req, res) => {
// res.setHeader('Content-Type', 'text/html');
// res.send( cache[1] );
// });
// https://www.runoob.com/nodejs/nodejs-express-framework.html
app.use(function (req, res, next) {
// JSON parse
// console.log('req.body', req.body);
// logger
// 获取主机名和IP地址
console.log('req.ip =', req.ip);
console.log('req.hostname =', req.hostname);
// 获取原始请求URL
console.log('req.originalUrl =', req.originalUrl);
next();
});
app.get('/', function (req, res) {
res.render('index', {
title: 'pug template engine 🚀',
message: '🐶 Hello Pug!',
});
});
// http://127.0.0.1:3000/api/get?q={'key':'value'}
// http://127.0.0.1:3000/api/get?q={key:'value'}
// http://127.0.0.1:3000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D
app.get('/api/get', (req, res) => {
// console.log('get req', req);
console.log('req.params', req.params);
console.log('req.query', req.query, req.query.q);
// console.log('req.query', req.query, JSON.stringify(JSON.parse(req.query), null, 4));
const obj = JSON.parse(req.query.q);
console.log('obj =', JSON.stringify(obj, null, 4));
res.setHeader('Content-Type', 'text/html');
res.send('get api');
});
app.post('/api/post', (req, res) => {
// console.log('❌ post req', req);
console.log('✅ post req.body', req.body);
// res.setHeader('Content-Type', 'text/html');
// res.send('get api');
res.sendStatus(200);
});
// http://127.0.0.1:3000/api/post
// {key: "value"}
app.post('/api/post', (req, res) => {
console.log('post req', req);
console.log('post req', req.params, req.query);
res.setHeader('Content-Type', 'application/json');
res.send({res: 'post api'});
});
app.listen(port, () => {
console.log(`
Server is running at http://${hostname}:${port}/
Server hostname ${hostname} is listening on port ${port}!
`);
});
fetch(`http://127.0.0.1:3000/api/post`, {
body: JSON.stringify({key: "value"}),
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "no-cors",
})
.then(res => console.log(`res =`, res))
.catch(err => console.error(`error =`, err));
http://127.0.0.1:3000/api/get?q={"username":"xgqfrms"}
express.js
4.17.1
5.0.0-alpha.8
https://expressjs.com/en/guide/migrating-5.html
HTML2Jade
HTML to Jade/Pug Online Realtime Converter
doctype html
html(lang='zh-Hans')
head
title= title
body
h1= message
section#app
a(href="http://127.0.0.1:3000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D") http://127.0.0.1:3000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D
middleware
https://github.com/expressjs/serve-static
//
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14683157.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2020-04-20 Angular Routing
2020-04-20 Angular 2020
2020-04-20 Flutter Widget API