ejs

Pass a variable from javascript to ejs

丶灬走出姿态 提交于 2019-12-01 17:29:27
I want to use a variable which is declared in javascript file to a ejs file. javascript: var express = require('express'); var app = express(); var myVar = 1; In the ejs file , where I want to use that variable inside a few if statements ,I have to declare it again in order to be able to use it. ejs file: var myVar = 1; if ( my Var ) .... How can I avoid this?Or is there a way for creating a configuration file which is accesible from both javascript and ejs? I tried also to use: app.locals.myVar = 1 but it is undefined in the ejs file. ------- UPDATE -------------------------- In my code I am

Displaying an image with EJS in node.js/express

余生长醉 提交于 2019-12-01 14:46:40
问题 I'm just trying to get setup with node.js/express/ejs. I know ejs isn't actual HTML and so I'm having a hard time just displaying a simple image. Can someone point me in the right direction? Directory structure is: myApp/server.js myApp/views/index.ejs myApp/logo.jpg Right now I have // index.ejs <img src = "../logo.jpg" /> Am I going about this the wrong way? Thanks. 回答1: Static files in Express must go inside the directory specified in your static middleware. This is commonly ./public/ .

NodeJS not able to get token value from req.params.token

微笑、不失礼 提交于 2019-12-01 10:35:16
问题 app.post('/reset/:token', function(req, res) { async.waterfall([ function(done) { User.findOne({ 'local.resetPasswordToken' : req.params.token, 'local.resetPasswordExpires' : { $gt: Date.now() } }, function(err, user) { if (!user) { req.flash('resetMessage', req.params.token); return res.redirect('back'); } ], function(err) { res.redirect('/'); }); }); app.get('/reset/:token', function(req, res) { User.findOne({ 'local.resetPasswordToken': req.params.token, 'local.resetPasswordExpires' : {

Node.js express: confuse about ejs template

核能气质少年 提交于 2019-12-01 09:18:05
I put my ejs template file in views folder like: views | |--foo.html | |--layout.html so I config my ejs template: app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderFile); I render my foo template by this: res.render('foo.html', {title: 'test'}); I want use layout.html as the master layout template, I also add <%- body%> tag in layout.html , but is doesn't work, what I saw is only return the ended foo.html . Why the layout.html can't be the master layout?Or how can I set it to the master layout? Ahah you just got tricked by Express 3 change in layout management.

Express' render/redirect does not work if the call isn't coming from a submit method in a form

落花浮王杯 提交于 2019-12-01 07:48:58
Task Perform a POST request from a JS method, so that variable values can be sent as parameters. Environment NodeJS Express BodyParser ejs My first attempt Frontend: <html> <head> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js'></script> <script type="text/javascript"> function postAnswer() { $.post('vote', { message: "hello!"}, function(returnedData) { console.log("Post returned data: " + returnedData); }); } </script> </head> <body> <button id='send' onClick='postAnswer()' class='btn btn-success btn-xs'>Svara</button> </body> </html> Server.js: var express =

Dynamic templates in EJS (node.js)

♀尐吖头ヾ 提交于 2019-12-01 07:18:24
问题 Does anybody know a nice solution for including templates, whose names would be known only at run time? EJS's includes allows only to specify exact file/template name. Use case : I have a article layout and there is article HTML/EJS content in some local file. To do so I need something like https://github.com/visionmedia/ejs#includes but file name should be taken from locals variable: Assume I have such handler (simplified): app.get('/article/:article', function (req, res) { var

Node.js express: confuse about ejs template

允我心安 提交于 2019-12-01 07:01:23
问题 I put my ejs template file in views folder like: views | |--foo.html | |--layout.html so I config my ejs template: app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderFile); I render my foo template by this: res.render('foo.html', {title: 'test'}); I want use layout.html as the master layout template, I also add <%- body%> tag in layout.html , but is doesn't work, what I saw is only return the ended foo.html . Why the layout.html can't be the master layout?Or how

Where to access and store EJS Helpers - SailsJS

て烟熏妆下的殇ゞ 提交于 2019-12-01 04:13:35
问题 Well, SailJS's default templateing engine is EJS ( Embedded Javascript ) But I cannot seem to find the place where we can create our own helpers and stuff. So, do you know where to access & store EJS helpers/stuff? 回答1: solved: https://github.com/balderdashy/sails/issues/2162#issuecomment-55866731 config/http.js module.exports.http = { // ... locals: { filters: { formatDate: function(date) { } } } } config/bootstrap.js _.extend(sails.hooks.http.app.locals, sails.config.http.locals); At some

Why is express telling me that my default view engine is not defined?

最后都变了- 提交于 2019-11-30 11:30:19
I'm using nodejs and mongodb in the back end for an app I'm working on. I'm using express to test the app, and I'm trying to use ejs to render my html files. However, I'm having the issue of my default view engine not being defined. Here is my app.js: /** * Module dependencies. */ var express = require('express') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path'); var conf = require('./conf'); var app = express(); var mongoose = require('mongoose'); , Schema = mongoose.Schema , ObjectId = mongooseSchemaTypes.ObjectID; var

【深入浅出Node.js系列十一】Node.js开发框架Express4.x

久未见 提交于 2019-11-30 09:16:34
#0 系列目录# 深入浅出Node.js系列 【深入浅出Node.js系列一】什么是Node.js 【深入浅出Node.js系列二】Node.js&NPM的安装与配置 【深入浅出Node.js系列三】深入Node.js的模块机制 【深入浅出Node.js系列四】Node.js的事件机制 【深入浅出Node.js系列五】初探Node.js的异步I/O实现 【深入浅出Node.js系列六】Buffer那些事儿 【深入浅出Node.js系列七】Connect模块解析 【深入浅出Node.js系列八】一个基于Node.js完整的Web应用实战 【深入浅出Node.js系列九】一起撸Node.js 【深入浅出Node.js系列十】一个简单的静态文件合并服务器 【深入浅出Node.js系列十一】Node.js开发框架Express4.x #1 建立项目# 让我们从头开始Express4.x的安装和使用吧,安装Node和NPM在本文就不多说了。Linux环境安装请参考文章, Node.js&NPM的安装与配置 ,Window环境安装直接下载Node的安装文件,双击安装就行了。 首先,我们需要安装express库 。在Express3.6.x之前的版本,Express需要全局安装的,项目构建器模块是合并在Express项目中的,后来这个构建器被拆分出来,独立成为了一个项目express