Node.js 虚拟主机
Nodejs web hosting
我正在使用 nodejs、js 和 html 制作一个选举软件。 Creently 我正在使用端口 3000 托管 public 文件中的静态文件,并在端口 4000 中侦听来自前端的编辑数据库的请求。我需要一种使用单个端口来完成这两项操作的方法。
更多解释:我有一个包含 html 和 js 代码的 public 文件,还有一个存储所有数据的 poll.json 文件。因此,我使用端口 3000 托管此 public 文件。在前端,我使用端口请求到端口 4000,发送投票详细信息以在数据库中进行编辑。我是托管服务器的新手,对各种方法感到困惑。
这是我现在的代码:
const createServer = require('http').createServer;
var express = require('express');
var app = express();
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
const listening_port=4000;
//const hosting_port=process.env.PORT || 3000;
const hosting_port=3000;
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
const server_listen = createServer((req, res) => {
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
body=body.split(",");
console.log(body.toString());
write_vote(body);
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
});
server_listen.listen(listening_port, () => {
console.log('Server listening on '+listening_port);
} );
var server_host = app.listen(hosting_port, 'localhost',function(){
console.log('listening for requests on port '+hosting_port);
});
//Static files
app.use(express.static('public'));
编辑:目前我已经像这样编辑了我的代码。
const createServer = require('http').createServer;
var express = require('express');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic('/public');
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
const listening_port=3000;
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
const server_listen = createServer((req, res) => {
if (req.method === "GET") {
fs.createReadStream("./public/index.html", "UTF-8").pipe(res);
}
else if (req.method === "POST") {
console.log('post request');
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
body=body.split(",");
console.log(body.toString());
write_vote(body);
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
}
});
server_listen.listen(listening_port, () => {
console.log('Server listening on '+listening_port);
});
但仍然无法主持 index.html。但我想托管整个公共文件。我尝试了来自互联网的 somoe 解决方案,它只会带来错误。我现在如何托管 public 文件?
您不需要导入 http 模块,因为 express 就足够了。您需要的是只使用一个端口号进行快递。您正在创建两个不同的服务器。 Express 足以创建一个服务器。
NOTE1- by setting app.use(express.static(__dirname+'/public'))
you are telling express to add all files inside public to url. E.g. image.png
in public folder can be accessed at localhost:3000/image.png
url
NOTE2- the below code will maybe not satisfy your need as it is not clear that what is inside body variable.
您必须至少阅读 here 中的 5 章。页面下方有下一步按钮可以转到下一章。
进一步阅读 this 之前会更清楚地阐明 things.read 5 章
你的代码应该像-
const express = require('express')
const app = express()
const port = 3000
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
//Static files
app.use(express.static(__dirname+'/public'));
//below will be your homepage(I.e. "localhost:3000/ ")
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/', (req, res) => {
let body=JSON.parse(req.body);
console.log(body.toString());
write_vote(body);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
});
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
我正在使用 nodejs、js 和 html 制作一个选举软件。 Creently 我正在使用端口 3000 托管 public 文件中的静态文件,并在端口 4000 中侦听来自前端的编辑数据库的请求。我需要一种使用单个端口来完成这两项操作的方法。
更多解释:我有一个包含 html 和 js 代码的 public 文件,还有一个存储所有数据的 poll.json 文件。因此,我使用端口 3000 托管此 public 文件。在前端,我使用端口请求到端口 4000,发送投票详细信息以在数据库中进行编辑。我是托管服务器的新手,对各种方法感到困惑。
这是我现在的代码:
const createServer = require('http').createServer;
var express = require('express');
var app = express();
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
const listening_port=4000;
//const hosting_port=process.env.PORT || 3000;
const hosting_port=3000;
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
const server_listen = createServer((req, res) => {
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
body=body.split(",");
console.log(body.toString());
write_vote(body);
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
});
server_listen.listen(listening_port, () => {
console.log('Server listening on '+listening_port);
} );
var server_host = app.listen(hosting_port, 'localhost',function(){
console.log('listening for requests on port '+hosting_port);
});
//Static files
app.use(express.static('public'));
编辑:目前我已经像这样编辑了我的代码。
const createServer = require('http').createServer;
var express = require('express');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic('/public');
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
const listening_port=3000;
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
const server_listen = createServer((req, res) => {
if (req.method === "GET") {
fs.createReadStream("./public/index.html", "UTF-8").pipe(res);
}
else if (req.method === "POST") {
console.log('post request');
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
body=body.split(",");
console.log(body.toString());
write_vote(body);
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
}
});
server_listen.listen(listening_port, () => {
console.log('Server listening on '+listening_port);
});
但仍然无法主持 index.html。但我想托管整个公共文件。我尝试了来自互联网的 somoe 解决方案,它只会带来错误。我现在如何托管 public 文件?
您不需要导入 http 模块,因为 express 就足够了。您需要的是只使用一个端口号进行快递。您正在创建两个不同的服务器。 Express 足以创建一个服务器。
NOTE1- by setting
app.use(express.static(__dirname+'/public'))
you are telling express to add all files inside public to url. E.g.image.png
in public folder can be accessed atlocalhost:3000/image.png
url
NOTE2- the below code will maybe not satisfy your need as it is not clear that what is inside body variable.
您必须至少阅读 here 中的 5 章。页面下方有下一步按钮可以转到下一章。
进一步阅读 this 之前会更清楚地阐明 things.read 5 章
你的代码应该像-
const express = require('express')
const app = express()
const port = 3000
let pollData = require(__dirname+'/public/poll.json');
var fs = require('fs');
var all_usn_no=[];
//Static files
app.use(express.static(__dirname+'/public'));
//below will be your homepage(I.e. "localhost:3000/ ")
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/', (req, res) => {
let body=JSON.parse(req.body);
console.log(body.toString());
write_vote(body);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
});
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}