[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 ...
随机推荐
- python django的单元测试
# TestCase 基类的使用 django.test.TestCase # 测试依赖于数据库访问,创建或查询模型 unittest.TestCase #没有与数据库交互 避免刷新数据库的成本
- 【Henu ACM Round #12 A】 Grandma Laura and Apples
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 知道题意之后就是一个模拟的过程了. 用int now记录当前苹果的个数.bool flag记录是否有小数(即半个苹果) (这样处理为 ...
- Javascript:存储和读取cookie
Cookie是网页开发中的一项重要技术,用于在本地存储一些信息(如username,password.登录状态)以便用户下一次訪问时使用(或在其他页面使用). cookie的格式是键值对,多个键值对之 ...
- 如何应对DDOS网络攻击
650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/a ...
- Dao层封装泛型实现(spring mvc,springjdbctemplate)
代码片段(6) [全屏查看所有代码] 1. [代码]BaseDao 跳至 [1] [2] [3] [4] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- loadrunner--设置代理录制
为了解决loadrunner录制不到脚本或录制经常无响应等情况,可以选用代理录制方式,步骤如下. 打开配置好代理的浏览器,输入要录制的服务器ip,开始操作,录制完成后点击Shutdown
- 微信支付v2开发(5) 订单查询
本文介绍微信支付中订单查询功能的实现. 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一.订单查询 因为某一方 ...
- Altium Designer的pcb界面如何让线变成点
但是16版本,需要tools --- Grid Manager --双击 双击后: 转自:http://blog.csdn.net/ldcung/article/details/77411434
- 如何使用 PyCharm 将代码上传到远程服务器上(详细图解)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶测试说明 1.通过Windows电脑上的PyCharm,将代码上传到虚拟机Ubuntu系统中 需要在虚拟机中安装Ubuntu的 ...
- Loadrunner11--输入license后提示违反许可证安全,禁止操作
安装中文补丁包后,重新把mlr5lprg.dll.lm70.dll覆盖LR11安装目录下“bin”文件夹下mlr5lprg.dll.lm70.dll.运行deletelicense.exe.重新用管理 ...