Node pug form button action not working from app.js - node.js

I have an index.pug page that is:
html
head
title= title
body
h1= message
form(method="get" action="/foo")
button(type="submit") run foo
and in my app.js I have:
app.get('/foo', function(req, res) {
console.log("Its come here");
res.render('index', { title: 'hello', message: 'hello' });
});
But when I click the button it does nothing. Am I doing something stupid?
Can't work out why any ideas appreciated. Thanks

Indent the button so that it is a child element of the form.
html
head
title= title
body
h1= message
form(method="get" action="/foo")
button(type="submit") run foo

html
head
title= title
body
h1= message
form(method="get" action="/foo")
input(type="submit", value="run foo")
button(type="submit") run foo replace it with input(type="submit", value="run foo") It will work

Related

Pug not rendering variables defined in res.render

Does anybody know why my tour variable is not rendered on the site. My paragraph is rendered but tour is undefined
app.get('/', (req, res) => {
res.status(200).render('base', {
tour: 'The Forest Hiker',
user: 'Eldin',
});
});
(base.pug file)
doctype html
html
head
title Natours
link(rel='stylesheet' href='css/style.css')
link(rel='shortcut icon' type='image/png' href='img/logo.png')
body
h1= tour
p This is just some text
I found the answer. It should be used #{tour}

Nodejs. Unknown symbols in browser

I am using express plugin in nodeJS
app.get('/',function(req,res) {
res.render('main', {
pageTitle: "заголовок",
header: "хедер"
});
});
I set jade for view engine
app.set('view engine','jade');
And this is my simple jade view
doctype html
html(lang="en")
head
title= pageTitle
body
h1= header
But i get unknown symbols in my browser.
http://pastenow.ru/Upload/Paste/FY0W.png
In your jade code , try to specify charset
doctype html
html(lang="en")
head
meta(charset="UTF-8")
title= pageTitle
body
h1= header

Why is my jade index page blank?

I'm broadly following this tutorial on Express, Mongo and Jade, and although I've successfully fetched back some data from mongo, but jade isn't rendering my page.
http://blog.ijasoneverett.com/2013/03/a-sample-app-with-node-js-express-and-mongodb-part-1/
Snippets are:
app.js:
app.get('/', function(req, res) {
employeeProvider.findAll(function(error, emps) {
// adding logging here shows that 'title' and 'emps' are correctly populated
res.render('index', { title:'Employees', employees:emps });
});
});
layout.jade:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
index.jade:
extends layout
block content
h1= title
div
each employee in employees
div.employee
div.created_at= employee.created_at
div.title= employee.title
div.name= employee.name
When I extract the source from the page displayed in the browser, it just shows this:
<!DOCTYPE html><html><head><title>Employees</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body></body></html>
In fact, nothing I put in jade.index to simplify it seems to get rendered. eg this also renders a blank page:
index.jade:
extends layout
block content
h1= title
Check again the tutorial and follow it (realy), since your code is diferent...
http://blog.ijasoneverett.com/2013/03/a-sample-app-with-node-js-express-and-mongodb-part-1/
The index.jade should be:
extends layout
block content
h1= title
#employees
- each employee in employees
div.employee
div.created_at= employee.created_at
div.title= employee.title
div.name= employee.name
a(href="/employee/new")!= "Add New Employee"
There are some needed css over #employees and
the each loop needs a - before itself.

I can not run nicEditor at Jade

Here is code, doesn't seem to an error
extends ../layout
block content
script(type='text/javascript', language='javascript', src='/js/nicEdit.js')
script bkLib.onDomLoaded(function() { nicEditors.allTextAreas() })
form(role='form', method='post', action='/blogs')
legend
div.form-group
textarea(name='area1')
button.btn.btn-primary(type='submit') Submit
button.btn.btn-default(type='reset') Clear
a.btn.btn-default(href='/blogs/admin') Back
You should try this, like the Jade Reference shows:
script(type='text/javascript', language='javascript', src='/js/nicEdit.js').
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
This should look like this example from NicEdit in HTML.

Linking to other jade files

I'm trying to understand how Express and Jade works.
First of all, am I doing it right when I'm using layout.jade as a template file (header, body, footer) and using different files to show information in the body (see examples below)?
The code works fine, but i'm unsure if this is the right way to do stuff in Express. If I should keep going with this structure, how can I link to other files (eg.About.jade) internally from for example index.jade, to show that file instead of index.jade?
Thanks in advance!
layout.jade:
!!! 5
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js')
script(type='text/javascript', src='/javascripts/external.js')
// Header
header#header
// Navigation
nav#nav
// Navigation code (ul, li etc)...
// Sidebar
aside#sidebar
// Sidebar code...
// Body
body!= body
index.jade:
!!! 5
html
head
title= title
section#wrapper
img.imageStyle(src = '/images/test1.png')
// And so on...
About.jade:
// You get it...
I think what you're looking for are view rendering routes in express:
http://expressjs.com/en/guide/using-template-engines.html
So you can set up something like this:
app.get('/', function(req, res){
res.render('index.jade', { title: 'index' });
});
app.get('/about', function(req, res){
res.render('about.jade', { title: 'about' });
});
To link from one to the other, once you have the proper routes configured, you can just do something like:
a(href='/') index
a(href='/about') about
Update Also, you don't need this repeated again in index.
!!! 5
html
head
title= title
additionally to what Wes Freeman wrote you can also include other jade templates in your jade file.
that way you could have your header.jade, footer.jade and include them in your about.jade file. here's the include documentation from jade:
https://github.com/visionmedia/jade#a13
that way you only have to change the header.jade file if you add for example script or stylesheet tags that should be on every page.

Resources