简体   繁体   English

如何在 Node JS 中向 html 页面发送响应?

[英]How to send response to html page in Node JS?

I am trying to visualize points on a map through NodeJs.我正在尝试通过 NodeJs 可视化 map 上的点。 Here is my index.js file:这是我的 index.js 文件:

var express = require('express');
var fs = require('fs');
var getdata = require('./database.js');

var app = express();

app.get('/', function (req, res, next) {
    res.writeHead(200,{'Content-Type':'text/html'});
    var myReadStream = fs.createReadStream(__dirname + '/index.html','utf8')
    myReadStream.pipe(res);
    //var value = getdata.deneme();
    //console.log("Value: "+value);
});
app.listen(4000, function () {
    console.log('Server is running.. on Port 4000');
});

I can call the map through HTML file.我可以通过 HTML 文件调用 map。 Index.html file is here: Index.html 文件在这里:

<html><body>
    <div id="mapdiv"></div>
    <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script>
      map = new OpenLayers.Map("mapdiv");
      map.addLayer(new OpenLayers.Layer.OSM());


      var lonLat = new OpenLayers.LonLat(32 ,39 )
            .transform(
              new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
              map.getProjectionObject() // to Spherical Mercator Projection
            );

      var zoom=16;

      var markers = new OpenLayers.Layer.Markers( "Markers" );
      map.addLayer(markers);

      markers.addMarker(new OpenLayers.Marker(lonLat));

      map.setCenter (lonLat, zoom);
    </script>
  </body></html>

The last step is connecting to database.最后一步是连接数据库。 I created a database.js file:我创建了一个 database.js 文件:

const{Pool,Client} = require('pg');

var connectionString = "postgressql://ID:password@ec2-54-228-243-29.eu-west-1.compute.amazonaws.com:5432/dduigib0uc8ebt?ssl=true";

const pool = new Pool({
    connectionString: connectionString,
  });


module.exports = {
    deneme: function() {
        pool.query('SELECT st_astext(loc) from GetAllData()', (err, res) => {
            console.log(err, res)
            return res
            pool.end()
        });
    }
};

I can connect to database and get the locations of points.我可以连接到数据库并获取点的位置。 But I don't know how to send these points to my HTML file to visualize the points on map.但我不知道如何将这些点发送到我的 HTML 文件以可视化 map 上的点。 In HTML page includes sample point for visualize.在 HTML 页面中包含用于可视化的示例点。 I just want to enter my points to there.我只是想在那里输入我的积分。 How can I do that?我怎样才能做到这一点?

Your back end should contain the database connection and the endpoints.您的后端应该包含数据库连接和端点。 You need to call those endpoints from your front end.您需要从前端调用这些端点。 The front end part should contain the HTML page and requests.前端部分应包含 HTML 页面和请求。 you can use Xml Http Request to handle the requests.您可以使用Xml Http Request来处理请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点 JS 如何将 Json 响应转换为节点 JS 中的 HTML 页面 - Node JS How to transform Json response to HTML page in Node JS 在Node.js中使用Express成功后,如何发送JSON响应并重定向到某些html页面? - How to send a JSON response and redirect to some html page after success in Node.js using express? 如何编写带有脚本的html页面以在node.js中进行响应? - How to write an html page with scripts to response in node.js? 如何在Node.js中发送响应 - How to send response in Node.js 节点 JS 和 Angular Email 验证:无论如何要发送 html 作为响应? - Node JS and Angular Email Verification: Anyway to send html in a response? 在服务器端Node.JS上倒计时并发送页面响应 - Count down to a time on the serverside Node.JS and send response of a page 如何在 AWS Lambda 节点 js 中发送 html 电子邮件,为 AWS api 网关返回格式正确的响应 - How to send a html email in AWS Lambda node js returning a well formed response for AWS api gateway 如何在 Node.js 的 HTML 页面上显示 API 响应? - How can I show the API response on an HTML page in Node.js? 如何将html数据从html页面发送到node.js服务器 - how to send json data from html page to node.js server 如何在 Node JS 的 nodemailer 中包含 HTML 页面 - how to include an HTML page in nodemailer in Node JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM