Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将nodejs var的值放在html中_Html_Node.js - Fatal编程技术网

将nodejs var的值放在html中

将nodejs var的值放在html中,html,node.js,Html,Node.js,我尝试使用nodejs读取IP地址并显示在html页面上,以下是我迄今为止所做的工作: app.js const express =require('express') const app = express(); var os = require( 'os' ); var path = require('path') app.get('/',function(req,res){ res.sendFile(path.join(__dirname+'/index.html'))

我尝试使用nodejs读取IP地址并显示在html页面上,以下是我迄今为止所做的工作:

app.js

    const express =require('express')
const app = express();
var os = require( 'os' );
var path = require('path')
app.get('/',function(req,res){
    res.sendFile(path.join(__dirname+'/index.html'))
  })

var networkInterfaces = Object.values(os.networkInterfaces())
    .reduce((r,a)=>{
        r = r.concat(a)
        return r;
    }, [])
    .filter(({family, address}) => {
        return family.toLowerCase().indexOf('v4') >= 0 &&
            address !== '127.0.0.1'
    })
    .map(({address}) => address);
var ipAddresses = networkInterfaces.join(', ')
console.log(ipAddresses);


app.get('/DHCP',(req,res)=>{
   return  networkInterfaces[1];

});

app.listen(1000)
以及index.html

    <!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>
<body>
    <p id="DHCP"  align="middle"> DHCP:</p>
</body>
</html>

页面标题

DHCP:

我是网络开发世界的新手,所以我不知道该怎么做!
提前谢谢

您需要选择一个(例如),然后使用根据模板引擎的语法修改的html模板进行调用。

使用如下方法:

<p id="DHCP" align="middle">{{DHCP:}}</p>

{{{DHCP:}


如果您以前将应用程序设置为使用HTML而不是任何视图引擎,则可以使用此功能。

您需要使用任何模板引擎,如ejs、Handlebar或Pugpable的副本