[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 ...
随机推荐
- 深入了解"网上邻居"原理
说到“网上邻居”,相信很多人都很熟悉.但是说起“网上邻居”的工作机制,可能大家就不太清楚了. 要说“网上邻居”的工作机制,不妨联系一下生活中的例子:比如我(A),要拜访一个远方的朋友(B),我要去他的 ...
- at&&atq&&atrm---定时任务
at放在 ls /var/spool/at/ 目录下 At的配置文件/etc/at.deny和/etc/at.allow 如果deny单独存在,则是deny以为的所有用户都可以使用at命令 如果all ...
- python爬虫之『入门基础』
HTTP请求 1.首先需要了解一下http请求,当用户在地址栏中输入网址,发送网络请求的过程是什么? 可以参考我之前学习的时候转载的一篇文章一次完整的HTTP事务过程–超详细 2.还需要了解一下htt ...
- Android经常使用自己定义控件(二)
经常使用的Android自己定义控件分享 http://www.see-source.com//androidwidget/list.html?type=&p=1
- 分享一段php获取随意时间的前一天代码
<?php /** * 获取给定日期的前一天 * @param string $date * @return string $yesterday */ function getYesterday ...
- 亚马逊AWS的route53的收费,月费最低 0.9 USD
亚马逊AWS的route53的收费Amazon Route 53 定价 https://aws.amazon.com/cn/route53/pricing/ 一文中,对于一些术语的解释第一项收费--域 ...
- onblur 对象失去焦点事件
onblur 对象失去焦点事件 一.总结 1.几乎所有的控件都支持onblur事件 二.简介 onblur 事件 Event 对象 定义和用法 onblur 事件会在对象失去焦点时发生. 语法 onb ...
- powerdesigner 连接mysql提示“connection test failed”
powerdesigner 连接mysql提示“connection test failed”,该如何解决: 1.把64位的jdk换成32位的jdk(VM只支持32的jre) 2.系统变量: CL ...
- Excel查询序列所相应的值-vLoopup函数,求比例分子改变但分母不变
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWV3ZWlvdXlhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- magento getCarriers 分析
完整的设置订单追踪信息的时候我们可能会用到它.在后台中他在这里设置: 有的时候我们想要设置自己定义的 carrier 比如 顺丰 申通 圆通 ..等等 我们能够先从 magento api 入手分析 ...