简体   繁体   中英

How to easily pass a variable with Node.js to the view

I'm trying to learn the basics of Node.js and I can't seem to be able to simply send a variable from app.js to index.html without using Jade or other template engine.

This is my app.js

var express = require("express");
var app = express();
app.get('/', function(req, res){

    //I'd like to send this as a variable instead    
    res.send("some text");

});
app.listen(8080);

This is my index.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        //I want to alert my variable here please
        alert(variableFromAppjs);
    </script>
</head>
<body>
    <p>Hello</p>
</body>
</html>

Is there a way to do it simply like that?

The reason you can't just "send" the variable from your app.js to index.html is because they're two separate programs. index.html is run ONLY on the client machine through the browser, and app.js is run ONLY on the server.

In order for index.html to receive data from app.js, you'll need to to use XMLHttpRequest to make a request to your Node app.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

You can then receive the data asynchronously and save it to whatever variable you want.

You will need to make sure you're using a server-side templating engine (express comes with jade support).

Use npm to install jade to your express app. and then tell express where the templates are, and, in the get route definition you must instruct express what view to use:

var express = require("express");
var app = express();
// Tell express where your templates are
app.set("views", __dirname + "/views");
// Tell express templates are jade files
app.set("view engine", "jade");
app.get("/", function(req, res) {
  res.render("home", {
    title: "Home Page",
    body: "This is just a test..."
  });
});
app.listen(8080);

when all this is setup create views/layout.jade and a views/home.jade

Home.jade will look like this:

extends layout
block content
  h1= title
  p A tag name followed by the equals (=) sign is like php echo
  p= body
  ul
    li This is a hardcoded list
    li With two elements

Layout.jade will look like this:

html
  head
    meta(charset="utf-8")
    title= title
    link(rel="stylesheet", href="/css/main.css")
    meta(name="viewport", content="width=device-width")
  body

    block content
      div.page-content.text-center
        h1 Default content
        p anything I put here will be overridden by the home.jade content block Which means that any jade view that doesn't provide a `block content` part will render this text instead.

If you create another route for example:

app.get("/default", function(req, res) {
  res.render("test", {title: "Default route", body: "Whatever"});
});

an then its corresponding template views/test.jade

extends layout
  p I'm not providing a `block content` so this text won't be rendered
  blockquote(class="my-class", id="my-id") In jade, the very first word of a line is the tag to be used by the line, to provide attributes to a tag you use comma-separated HTML attributes in between parenthesis right next to the tag name.

And visit your route at http://localhost:8080/default You will see the default text be rendered.


I hope this is of help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM