0

I want to render some HTML text within my express route. I know one of the options is using the npm-needle module but I was unsure if we there is any way to use npm-express and npm-http within the same route defined.

What I want is something like:

var http = require("http");
var express = require("express");
var app = express();

app.get("/", function (req, res) {
  var params = req.params;
  var url = req.query["url"];
  let handleRequest = (request, response) => {
    response.writeHead(200, {
      "Content-Type": "text/plain",
    });
    response.write("Hi There! " + url);
    response.end();
  };
});
app.listen(5000);
//http.createServer(handleRequest).listen(8000); ---> not using this in the code

Is something of this type possible? Thanks!

Abhinav Sharma
  • 147
  • 2
  • 9
  • No, what you show is not possible. You can't define `handleRequest()` inside a route and then use it elsewhere. You can use `res.send("some text");` inside your route handler if you want, but you'd have to better describe what you're trying to do. Remove the `http.createServer()` line entirely. Your http server is already created inside of `app.listen()`. – jfriend00 Jun 20 '20 at 20:25
  • Okay so I followed https://stackoverflow.com/questions/29328028/node-js-passing-parameters-to-client-via-express-render but I am not able to render my ``url`` variable in the browser. res.send(\`

    {{url}}

    \`)
    – Abhinav Sharma Jun 20 '20 at 20:34
  • 1
    If you tried something like is in that question, you will have to show the actual code you tried in your question. What you have now in your question doesn't look anything like what's in that question. You see to be mixing up a bunch of things. You can send your URL with just `res.send('

    ' + url + '

    ');`. The `{{url}}` syntax involves using a template engine to render your page which is something else entirely. You can't just grab little pieces of code from random questions and insert them in your project. You need to understand how things actually work.
    – jfriend00 Jun 20 '20 at 20:35
  • If you write a clear question (in your question, not in the comments), that explains what you're trying to do, perhaps people can help you with the original problem. – jfriend00 Jun 20 '20 at 20:39
  • Thanks @jfriend00 for the help! This was something very basic but I got a bit overwhelmed, I'll keep that in mind from next time! – Abhinav Sharma Jun 20 '20 at 20:40

2 Answers2

1

I don't get why you do have this handleRequest function inside your route as you can use the req and resinside of this function inside your route.

If you want to deliver html from within your route you could send back a html file like this:

const path = require('path');

app.get("/", function (req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

or you could send back directly html-tags from within your route like this:

app.get("/", function (req, res) {
    res.send('<h1>Text</h1>')
});

Of course you can work with template strings etc. to get your data displayed.

Dominik S.
  • 81
  • 5
0

You can easily do it by request npm package.

const request = require('request');

app.get("/", function (req, res) {   
  request('http://www.google.com', function (error, response, body) {
    console.error('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response 
  
    console.log('body:', body); // Print the HTML for the Google homepage.
});
Erenn
  • 625
  • 6
  • 18
  • The request package has been deprecated for a variety of reasons. People probably shouldn't be writing new code with `request()`. There are plenty of [alternatives](https://github.com/request/request/issues/3143) that fully support promises (which is how one should be writing asynchronous code these days). – jfriend00 Jun 20 '20 at 20:10
  • He is clearly a beginner. I am sure he is not shipping enterprise software. It's still good for learning. – Erenn Jun 20 '20 at 20:12
  • 1
    Actually no. It's not good for learning. One should be learning how to program asynchronously with promises and if you're going to learn a new library, it might as well be one that isn't deprecated and one that supports promises that you can continue to use for a long time in projects that might actually matter. I would personally recommend the [`got()` library](https://www.npmjs.com/package/got), but there are others in my previous link of alternatives. – jfriend00 Jun 20 '20 at 20:17
  • Dude is listening two different ports for a http request but whatever [bluebird](https://www.npmjs.com/package/bluebird) is a good alternative. – Erenn Jun 20 '20 at 20:24
  • 1
    And why do you think Bluebird is needed here when promises are built into node.js now? – jfriend00 Jun 20 '20 at 20:27
  • I prefer to do my http-requests with axios as the API is pretty nicely build. And I think also, that he shouldn't write new code with the `request`-package as it's deprecated. – Dominik S. Jun 20 '20 at 20:46