[Node.js] Provide req.locals data though middleware
We can create Template Helpers, which can contains some common reuseable data and libs.
/*
This is a file of data and helper functions that we can expose and use in our templating function
*/ // FS is a built in module to node that let's us read files from the system we're running on
const fs = require('fs'); // moment.js is a handy library for displaying dates. We need this in our templates to display things like "Posted 5 minutes ago"
exports.moment = require('moment'); // Dump is a handy debugging function we can use to sort of "console.log" our data
exports.dump = (obj) => JSON.stringify(obj, null, 2); // Making a static map is really long - this is a handy helper function to make one
exports.staticMap = ([lng, lat]) => `https://maps.googleapis.com/maps/api/staticmap?center=${lat},${lng}&zoom=14&size=800x150&key=${process.env.MAP_KEY}&markers=${lat},${lng}&scale=2`; // inserting an SVG
exports.icon = (name) => fs.readFileSync(`./public/images/icons/${name}.svg`); // Some details about the site
exports.siteName = `Now That's Delicious!`; exports.menu = [
{ slug: '/stores', title: 'Stores', icon: 'store', },
{ slug: '/tags', title: 'Tags', icon: 'tag', },
{ slug: '/top', title: 'Top', icon: 'top', },
{ slug: '/add', title: 'Add', icon: 'add', },
{ slug: '/map', title: 'Map', icon: 'map', },
];
Then you can define a locals data in middleware:
Require helper file:
const helpers = require('./helpers');
// pass variables to our templates + all requests
app.use((req, res, next) => {
res.locals.hlp = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});
Then in the pug file, you can use those locals variable:
extends layout block content
h2 Sale ends in #{hlp.moment().endOf('day').fromNow()}
[Node.js] Provide req.locals data though middleware的更多相关文章
- 解决Postman发送post数据但是Node.js中req.body接收不到数据的问题[已解决]
之前编写后台接口,测试数据都是使用的Postman,相当的方便,之前也一直使用get方法,编写Node.js一直没有问题,但是由于要编写一个注册/登陆的功能,所以发送的post数据,后台的逻辑已经编写 ...
- [Node.js] Level 7. Persisting Data
Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...
- Connect is a middleware layer for Node.js
Connect is a middleware layer for Node.js http://senchalabs.github.com/connect Connect Connect is ...
- 了不起的Node.js读书笔记
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 第二章 Js概览 基于GoogleV8引擎 Object.keys(o) 数组方法:遍历forEach.过滤filter ...
- Node.js内置的文件系统模块(fs)
异步读取文件 按照js的标准,异步读取一个文本文件的格式如下: 'use strict' const fs = require('fs') fs.readFile('test.txt', 'utf-8 ...
- Node.js中,获取req请求的原始IP
Node.js代码 var express = require('express'); var app = express(); var http = require('http'); var ser ...
- Node.js高级编程读书笔记 - 4 构建Web应用程序
Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...
- Node.js开发者最常范的10个错误
目录 前言 1 不使用开发工具 1.1 自动重启工具 1.2 浏览器自动刷新工具 2 阻塞event loop 3 频繁调用回调函数 4 圣诞树结构的回调(回调的地狱) 5 创建一个大而完整的应用程序 ...
- Node.js 手册查询-4-Express 方法
express 标签(空格分隔): node.js express [TOC] 安装: 新版本中命令行工具分家了 npm install -g express //安装 express 然后 npm ...
随机推荐
- VMware Vsphere 6.0安装部署 vCenter Server安装
几个不同的组件 vCenter Server:对ESXi主机进行集中管理的服务器端软件,安装在windows server 2008R2或以上的操作系统里,通过SQL 2008R2 或以上版本的数据库 ...
- linux系统常用日志
系统日志记录着系统运行中的记录信息,在服务或者系统发生故障的时候,通过查询系统日志,可以帮助我们诊断.系统日志可以预警安全问题,系统日志一般都存放在/var/log目录下 /var/log/dmesg ...
- Oracle Sqlplus中上下键出现^[[A乱码问题
安装rlwrap 下载:http://utopia.knoware.nl/~hlub/uck/rlwrap/ 或者 百度云盘:http://pan.baidu.com/s/1ntM8YXr 须要先安 ...
- 根证书 CA
根证书 CA 密钥没有密码,使用下面的指令添加密码 openssl rsa -aes256 -in cakey_nopw.pem -out cakey_pw.pem cacert.pem -----B ...
- C++ 为什么要virtual析构函数
class A { public: A() { printf("A()\n"); } virtual ~A() { printf("~A()\n"); } }; ...
- 35.Intellij IDEA设置忽略部分类编译错误
转自:https://www.aliyun.com/jiaocheng/290360.html 有些时候我们的项目中有些错误,但这些错误并不影响项目的整体运行(或许是没有使用到),默认情况下idea是 ...
- 企业部署Linux应用将拥有更低的TCO
650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...
- Atcoder At Beginner Contest 068 D - Decrease (Contestant ver.)
D - Decrease (Contestant ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem S ...
- JS/CSS 在屏幕底部弹出消息
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- Eclipse工具修理集
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 俗话说:工欲善其事.必先利其器,eclipse偶尔会发发脾气,那我们要记录下"她" ...