[英]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.