Express JS render webpage on a new tab - node.js

I have a code which uses nodeJS for a web app using ExpressJS. A sample of the code which redirects the user to a webpage is:
app.post('/accept',(req,res)=>{
res.render("home");
});
The code works fine and redirects the user to the said page, but I want that to happen on a new tab, as in it will redirect the user to the "home" page on a new tab. How do I do that?

Your script is server side and makes a response to the client. You can't define there to open a new tab. So you have to handle that in your frontend via javascript.

Related

How to use res.redirect to redirect to a data url?

I need to use the res.redirect() to redirect to a data URL using express.
This is my code...
app.get("/",(req,res)=>{
res.redirect("data:text/plain;base64,hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
})
When I use another URL eg: https://www.google.com It works fine.
But when I use a data URL it is not working..
How should I fix it?
I am new to programming and StackOverflow. So, please be kind to me
You could redirect the user to a blob of the dataurl location.
For that just search google for dataurl to blob in js etc.
And also as you are using nodejs you could use the http.request and pipe the response to client just like proxying.
or you could fetch that file with "http.request" and save it in your server and give it when the client needs it
This is likely Chrome issue rather than NodeJS:
https://groups.google.com/a/chromium.org/g/blink-dev/c/GbVcuwg_QjM/m/GsIAQlemBQAJ
In practice, these will be blocked:
Navigations when the user clicks on links in the form of
window.open(“data:…”)
window.location = “data:…”
Meta redirects

Open browser popup on APIrequest nodejs express

I want to open a browser popup for client site on rest api request to nodejs backend.
I had tried
res.setHeader('Content-Type', 'text/html');
res.render('index', { title: 'Hey', message: 'Hello' });
But it still returning html codes as data to the client.
I also used window.open but window is not defined in server side
Is there anyway to make my backend redirect or render html form on api request!
the following image is how the client get response
If you want the browser change page/view, you need something like location.href = /yourview.html
If you want fill your popup with html built on the server, you need to get it using fetch or XMLHttpRequest or something built on top of them (for example axios, like you did) and then attach to the dom.
Once you got it, you can show the popup. But you are on the client side.
res.render return rendered html. http://expressjs.com/en/api.html#app.render

want to write node.js http client for web site testing

I am new to node.js
I want to try to write node.js client for my web site testing
(stuff like login, filling forms, etc...)
Which module should i use for that?
Since I want to test user login following other user functionality
it should be able to keep session like browser
Also any site where it has example of using that module?
Thanks
As Amenadiel has said in the comments, you might want to use something like Phantom.js for testing websites.
But if you're new to node.js maybe try with something light, like Zombie.js.
An example from their home page:
var Browser = require("zombie");
var assert = require("assert");
// Load the page from localhost
browser = new Browser()
browser.visit("http://localhost:3000/", function () {
// Fill email, password and submit form
browser.
fill("email", "zombie#underworld.dead").
fill("password", "eat-the-living").
pressButton("Sign Me Up!", function() {
// Form submitted, new page loaded.
assert.ok(browser.success);
assert.equal(browser.text("title"), "Welcome To Brains Depot");
})
});
Later on, when you get the hang of it, maybe switch to Phantom (which has webkit beneath, so it's not emulating the Dom).

Can Node js redirect to a url and display it in another new browser?

I want to use node js redirect to the url and display the url in a new browser, can that be happening?
The easiest way to do this cross browser is to use the "open" npm module
This module allows you to open a new browser window and designate a url to go to
npm install open --save
then in your request when you want to open the new browser and load the url simply call
open( 'http://urltodirect.to', function (err) {
if ( err ) throw err;
});
That will open a new browser window and send the tab to http://urltodirect.to
Hope this helps!
Node Js is a server side technology which can redirect the browsers request to a new location but cannot instructed the browser to open a new window.
You need to use client side javascript to ask the browser to open another window see here:
http://www.w3schools.com/jsref/met_win_open.asp
or use target="_blank" within the link itself.
You can redirect to a new URL with Node as part of a server request if you use the Express framework's redirect method.
However you can't display the URL in a new browser with Node, as this is front-end work. You could use target=_blank within your HTML link in order to do this.
One small trick that works for is,
I have a redirect on the link which I want to open in a new tab.
And added target="_blank" in my form
<form method='post' action="/post/URL" target="_blank">
...
</form>

how to check if my javascript browser extension works

I am trying to create a simple javascript based extension for Google Chrome that takes data from one specific iframe and sends it as part of a POST request to a webpage.
The web page that sends the data submitted by POST request, to my email address.
I tried running the extension, it looks to be running fine, but I am not getting any email.
The servlet which receives form data is very simple, I dont think there is any error in it.
What I want is some way to check if the javascript based extension works or not.
The javascript code is given below-
var mypostrequest=new ajaxRequest()
mypostrequest.onreadystatechange=function(){
if (mypostrequest.readyState==4){
if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("result").innerHTML=mypostrequest.responseText
}
else{
alert("An error has occured making the request")
}
}
}
var namevalue=encodeURIComponent("Arvind")
var descvalue=encodeURIComponent(window.frames['test_iframe'].document.body.innerHTML)
var emailvalue=encodeURIComponent("arvindikchari#yahoo.com")
var parameters="name="+namevalue+"&description="+descvalue &email="+emailvalue
mypostrequest.open("POST", "http://taurusarticlesubmitter.appspot.com/sampleform", true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
mypostrequest.send(parameters)
UPDATE
I made changes so that the content in js file is invoked by background page, but even now the extension is not working.
I put the following code in background.html:
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript( null, {file: "content.js"});
});
chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
</script>
Looking at your code looks like you are trying to send cross domain ajax request from a content script. This is not allowed, you can do that only from background pages and after corresponding domains are declared in the manifest. More info here.
To check if your extension works, you can open dev tools and check if there any errors in the console. Open "Network" tab and see if request was sent to your URL. Place console.log in various places in your code for debugging, or use full featured built in javascript debugger for step-by-step debugging.

Resources