I'm trying to build a login system with Nodejs and express. I have a login page with the login forms using ejs.
In my index.html file there's a button that's supposed to direct the user to that login page:
<button id="loginBtn">
<LI> LOGIN </LI>
</button>
but it prints the ejs code of the login page instead of displaying the actual page. Anyone know what I'm doing worng?
To render a view, you only specify an endpoint in expressjs and call only that end point
You should not try to call the .ejs file location
Example: in this code login.ejs will automatically render behind the scene, the server library will do it, res.render('login') will automatically call login.ejs
router.get('/home', function(req, res, next) {
res.render('login');
});
You hyperlink should be calling the router mapping endpoint alone
<li> LOGIN </li>
once you decided using EJS, for UI designing purpose, if the UI designing part alone is given to you, then you can do a dummy code to render one EJS with fixed data, you don't have to run the entire website with DB
For example: there a data which is assumed which will render a EJS, to render with dummy data you can do this
Get the data from the developer who is doing his part and do this code
router.get('/home', function(req, res, next) {
var data = {"name": "Emmanuel"}
res.render('login', data);
});
as you can see the data is directly coded temporary
Related
I want to redirect to another web page when I click a button. Please find the following code
js
app.controller('myCtrl', ['$scope', '$location', function($scope, $location) {
$scope.redirect = function()
{
console.log($location.url());
};
}
I can obtain the current url using $location.url() which is follows
http://IP:Port/?myId=this-is-my-test-page
What I want to do is i want to redirect to the following page when i click a button
http://IP:Port/?myId=this-is-my-test-page2
I want to edit the existing url inside the controller without hard coding the whole url as IP and Port can be changed. How can I achieve this?
Although I used $location.path("http://IP:Port/?myId=this-is-my-test-page2");
What is does is it appends to the current url as follows
http://IP:Port/?myId=this-is-my-test-page2#http://IP:Port/?myId=this-is-my-test-page2
How can I redirect to the required page ?
I had a not so similar issue where I had to convert the variable to a string and then modify the string. Then convert back to the proper data type.
Angular has its own routing. Everything right of # is the Angular routing (client side routing). Everything to the left of the # sign is your MVC/WebApi routing (server side routing). This is the essence of a single page application.
If you want to change the server side routing you need to do the redirect on the server side, so do a request to the server to change the url. If you want to change the client side (Angular) routing you need to use the "$location.path('')" in your angular code. Be aware that you cannot change anything to the left of the # sign on the client side.
You are trying to change the server side piece of the url, so you need to do a request to the server to change the url.
See this question for more information about the two:
I tried using $window.location and it was successful. Non of the other methods worked.
app.controller('myCtrl', ['$scope', '$window', function($scope, $window) {
$scope.redirect = function()
{
$window.location = "http://IP:Port/......";
};
}
I am making a webpage and I have been looking on stackoverflow on how to link to .ejs-files from a .html-file.
People are saying the following
In index.html:
<li>Twitter</li>
In script.js:
app.get('/twitter',function(req,res){
res.render('twitter', { });
});
But it does not work for me. It says "Your file was not found". It works on localhost, but not when I first click on index.html and then click on twitter.ejs page from there. My code looks like this: jsfiddle.
twitter.ejs is in a view folders, while index is outside of this folder.
What is wrong?
The situation is you're trying to link directly to a view template.
You need to create a route to a twitter controller that then calls your template. It would look something like this:
router.get('/twitter', twitterController.twitter_view);
If your .ejs file is called "contact.ejs" as an example, then the route you're targeting may be something like "/contact".
First setup the contact route in your server code:
//Setting up the contact route
app.get("/contact", function (req, res) {
res.render("contact", {});
});
Update the href in the corresponding anchor tag to:
href="/contact"
I am using expressJS and EJS for this example. So these must be setup first.
I'm building a fairly basic webpage using express. However, I'm having some trouble with my image pathways.
This code works fine.
app.use(express.static(path.join(__dirname, "/app/public/")));
app.get("/overview", function(req, res) {
res.render('some-file');
});
Inside of some-file.ejs I have...
<img src="assets/images/picture.jpg">
But what doesnt work is when I have a second url pathway.
app.get("/overview/specific", function(req, res) {
res.render('another-file');
});
<img src="assets/images/picture.jpg">
In this example I'm trying to load the exact same image (in my case its a banner thats reused on every single page). This gives me an error that the image is not found. What I've noticed from the console errors is that the image is being loaded from localhost:3000/overview/assets/images/picture.jpg
I don't understand why express is trying to load the image from whatever the first pathway is (overview in this case). Overview shouldnt be in the pathway!
Can anyone help me out debugging this issue?
Thanks in advance
Try to use /assets/images/picture.jpg.
Add / before the path. Then it will take /app/public/ as a root and be sure that the image will be at :
/app/public/assets/images/picture.jpg
Now wherever you want picture.jpg just pass this absolute path.
We serve favicons dynamically using an ExpressJS redirect, it works very well.
First, we retrieve the site object from memory with a quick lookup based on req.hostname, then send this response:
res.redirect(site.favicon);
The favicon variable could be a static asset on our server, or an asset on another server too. Our front-end code just calls /api/resources/favicon and it will receive the correct link in return.
I'm trying to send a hyperlink (such as: "http://google.com") as a parameter to my Express server script. My current script looks like this:
var app = require("express")();
app.get("/new/:link(*)", function(req, res){
var link = req.params.link;
res.end(JSON.stringify({
site: link
}));
});
app.listen(process.env.PORT || 3000, function(){
console.log("Listening...");
});
This is just a test to see if I can get it working so I can build something bigger on top. The idea is that I can send a link and receive the link in JSON. However when I try to go to the site with the the link as parameter, my browser want to save a file called "google.com" and it doesn't receive any JSON from the server.
I know it's possible to do this without changing anything about my browser but I don't know how. Anyone has any ideas?
Ok, so I have accidentally fixed my problem.
Apparently i had to write "res.send(...)" instead of "end". It now works perfectly although I don't really understand why.
In my Onsen app I have the following splitter. I am using Jade, and rendering all the other pages from the list items in html (despite the fact that they are in separate jade files) by including the files at the bottom of the page, as shown below:
body(ng-controller='...')
ons-splitter(var='mySplitter')
ons-splitter-side(var='menu' side='left' width='220px' collapse swipeable)
ons-page
ons-list
ons-list-item(ng-click="root.load('home.jade')", tappable='')
| Home
ons-list-item(ng-click="root.load('search.jade')", tappable='')
| Search
... more list items
ons-template(id='home.jade')
ons-page(ng-controller='...')
ons-toolbar
.left
ons-toolbar-button(ng-click='mySplitter.left.open()')
ons-icon(icon='md-menu')
.center
| My App
//- google maps stuff
ons-input#pac-input.controls(type='text', placeholder='Search Box')
div#map.col-md-12
ons-bottom-toolbar
.center
| MyApp
include search.jade
I believe this is a dirty shortcut, and will load the contents of search.jade (as well as every other file I include) before the user even clicks the item in the splitter.
I do not want this functionality. I would like to instead have server code in NodeJs that renders the jade files in html when they are ready to be displayed to the user. Something like this:
jade.renderFile('search.jade')
This angular code is currently how I am loading the page from the item in the splitter:
mySplitter.content.load(page)
.then(function() {
$scope.pop = page;
mySplitter.left.close();
});
However I am very confused about how to write this in a node route. Do I just abandon the splitter function in angular?
Can anyone help clarify this for me and show me a clear example of how to write the node route to render the jade files as html each time they are loaded?
Please see solution 1 of the selected answer from this stack overflow post for a reference of what exactly I am trying to do: stack overflow post
I am currently using solution 2 from that post.
I believe this is the relevant code in server.js:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
However when I change jade to html, I receive the error:
Error: Cannot find module 'html'
All my front-end files in the views folder have .jade extensions and are written in jade.
Update
Here is how I am serving index.jade (which is in the views folder) in a file called index.js:
module.exports = function(app){
/* Get home page. */
app.get('/', function(req, res, next) {
res.render('index', { title: 'My App' });
});
}
This was my old route NodeJS route which is no longer being used because of the splitter:
// get user search page
app.get('/user/search', function(req, res, next) {
return res.render('searchForTrainer');
});
Hmm. Since your code seems relatively small I would guess that what it does may be just serving all your files from views and actually "rendering" them. So probably you are just failing to access them properly later on. Maybe you have a url like /search.html or /search (instead of /search.jade). Could you try to confirm whether you can access such a url?
Also is your index.jade file served in some other way like startingPoint: 'index.jade' or something similar or is it also located in the views folder?
Basically as long as your index file has the same treatment as your other views then everything should be fine.
Update:
With what you just provided we can see the way in which you are serving your index.
app.get('/', function(req, res, next) {
res.render('index', { title: 'Fitness App' });
});
The equivalent of that is exactly the same as what you said you had before:
app.get('/user/search', function(req, res, next) {
return res.render('searchForTrainer');
});
Here res.render is what converts your jade into html and then returns it to the client. Since the splitter is expecting html that means you shouldn't have made changes to the server when you started using it.
Here is how the process looks like:
Client | HTTP | Server
| |
content.load('page') → GET /page ↘
| | res.render('page') // convert jade to html
html is loaded ← 200 OK html content ↙
in splitter.content | |
TL;DR - if you use your old route everything should be fine. Just remember to change the page url in the splitter from search.jade to /user/search (or whatever the url for will be).