我正在尝试创建一个Node.JS网站。我想从json文件中检索数据以正确显示在网站上。我该怎么做。我是Node.JS的新手,不了解我在哪里编写脚本。
{
"id": "dfd9722a-3ca3-48e7-af2b-633e33d626b5",
"clientId": "4ef8da99-07ee-4f40-8ea5-be13f4592a62",
"source": "D346618B-2C9F-4765-940A-B9369BD67114",
"destination": "",
"priority": "MEDIUM",
"reliability": "BEST_EFFORT",
"eventTime": 1535036080788,
"eventTimeAsString": "2018-08-23T14:54:40Z",
"sender": "",
"type": "DATA",
"properties": {},
"direction": "FROM_DEVICE",
"receivedTime": 1535036083635,
"receivedTimeAsString": "2018-08-23T14:54:43Z",
"payload": {
"format": "urn:oracle:hospitality:lock:attributes",
"data": {
"locked": false,
"Source_FirstName":"John",
"Source_LastName":"Dunbar"
}
}
}
我正在尝试让 John (源名字) Dunbar (源名字)出现在我的网站上。我正在使用Node.JS,express和handlebars(作为我的模板引擎)。
<div id="body-container">
<div id="header">
<div id="logo-content">
<div id="logo"></div>
</div>
<div id="welcome">
<p>Welcome {{!-- <-- Here goes JSON values }} to Node Park</p>
</div>
</div>
{{!-- end of header --}}
这是我的Node.JS代码
const express = require('express');
const path = require('path');
const hbs = require('express-handlebars');
// Init App
const app = express();
// Load View Engine
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout',
layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(__dirname+'/public'));
// Home Route
app.get('/', function(req, res){
res.render('index', {
title: 'Node.JS',
hotel: 'NODEMON'
});
});
// Start Server
app.listen(5000, function(){
console.log('Port 5000 open for business.');
});
答案 0 :(得分:2)
您可以在服务中需要CREATE PROCEDURE insert_data()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE iterations INT DEFAULT 0;
DECLARE rowOffset INT DEFAULT 0;
DECLARE limitSize INT DEFAULT 10000;
SET iterations = (SELECT COUNT(*) FROM Table1) / 10000;
WHILE i <= iterations DO
START TRANSACTION;
INSERT IGNORE INTO Table2(id, field2, field3)
SELECT f1, f2, f3
FROM Table1
ORDER BY id ASC
LIMIT limitSize offset rowOffset;
COMMIT;
SET i = i + 1;
SET rowOffset = rowOffset + limitSize;
END WHILE;
END$$
DELIMITER ;
文件。
json
并在html中访问//If the json file is in the same root
const json = require("./data.json");
app.get('/', function(req, res){
res.render('index', {
title: 'Node.JS',
hotel: 'NODEMON',
firtsName : json.payload.data.Source_FirstName,
lastName : json.payload.data.Source_LastName
});
});
和firtsName
lastName
答案 1 :(得分:-1)
const json = require('./your.json');
// Home Route
app.get('/', function(req, res){
res.render('index', {
title: 'Redwood Welcome',
hotel: 'Redwood Hotel',
source_firstname: json.source_firstname,
source_lastname: json.source_lastname,
});
});
<div id="welcome">
<p>Welcome {{source_firstname}} {{source_lastname}} to Node Park</p>
</div>