早上好。 我为用TypeScript编码的Web应用程序购买了VPS和域名。我设置了服务器(我安装了npm,express,pm2)...当我从浏览器中输入www.domainname.fr:4201时,我从编码到server.js的路由中获得了所需的json。
在本地,我的服务器端正在侦听端口4201,但是我的客户端正在侦听端口4200。当我使用浏览器www.domainname.fr:4200进行相同操作时,该域不接受的服务器连接。
我是服务器初学者。首先,必须键入端口号才能进入应用程序,这很丑陋。我希望输入www.domainname.fr直接进入我的index.html。第二件事,我怎样才能访问我的index.html?我尝试了很多代码,但没有一个呈现我的主页。
api.get('/', (req, res) =>{
const indexPath = path.resolve("../src/index.html")
res.render(indexPath);
})
我的服务器。 js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const nodemailer = require('nodemailer');
app.use(bodyParser.json());
app.use(express.static(__dirname+ '/img'));
app.use(express.static(__dirname+ '/docs'));
app.use((req, res, next) =>{
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers','Content-type');
next();
})
// localisation des ressources
let data = require("./res/curriculum");
let navRes = require("./res/navBar");
let textRes = require("./res/textes");
let hobbiesRes = require("./res/about_Hobbies");
let assetsRes = require("./res/about_Assets");
let fileRes = require("./res/files");
let buttonsRes = require("./res/buttons");
let newsRes = require("./res/news")
//import des ressources
let curriculumData = data.curriculum;
let navData = navRes.nav_elements;
let textData = textRes.textes;
let hobbiesData = hobbiesRes.hobbies;
let assetsData = assetsRes.assets;
let filesData = fileRes.files;
let dlButtonsData = buttonsRes.dl_buttons;
let newsData = newsRes.news;
// requêtes
const GetNewsData = () =>{
return [...newsData]
}
const GetDlButtonsData = () =>{
return [...dlButtonsData]
}
const GetCurriculumData = () =>{
return [...curriculumData];
}
const GetNavData = () =>{
return [...navData];
}
const GetTextData=() =>{
return [...textData];
}
const GetAssetsData=() =>{
return [...assetsData];
}
const GetHobbiesData=() =>{
return [...hobbiesData];
}
const GetFilesData=()=>{
return [...filesData];
}
const GetLongMonthFromNumber=(number)=>{
switch(number){
case 1, 01: return "Janvier"
case 2, 02: return "Février"
case 3, 03: return "Mars"
case 4, 04: return "Avril"
case 5, 05: return "Mai"
case 6, 06: return "Juin"
case 7, 07: return "Juillet"
case 8, 08: return "Aout"
case 9, 09: return "Septembre"
case 10: return "Octobre"
case 11: return "Novembre"
case 12: return "Décembre"
}
}
const mailer = nodemailer.createTransport(
{service:'Gmail',
auth:{
user:'phmerle.logikal@gmail.com',
pass:'Alk5-9pKF'
}
}
)
const api = express.Router();
api.get('/', (req, res) =>{
const indexPath = path.resolve("../src/index.html")
res.render(indexPath);
})
api.get('/news', (req, res) =>{
let jsonArray= [];
for(let entry of GetNewsData()){
const text = fs.readFileSync(entry.path, 'UTF-8')
const date = entry.path.split('_');
const year = date[1].substr(0,2);
const month = GetLongMonthFromNumber(parseInt(date[1].substr(2,2),10));
const jsonObject = {id:entry.id, year, month, text}
jsonArray = [ jsonObject,...jsonArray]
}
res.json(jsonArray);
})
api.get('/dl_buttons', (req, res) =>{
res.json(
GetDlButtonsData()
)
})
api.get('/curriculum', (req, res) => {
curriculumData = data.curriculum;
formationData = data.formation;
res.json(
GetCurriculumData()
);
})
api.get('/nav', (req, res) =>{
res.json(GetNavData());
})
api.get('/dl/:fileId', (req, res)=>{
const fileId = parseInt(req.params.fileId,10);
const file = GetFilesData().filter(f=>f.id === fileId)
const path = file[0].path;
res.download(path);
})
api.get('/img/*', (req, res)=>{
res.sendFile(req.url, {root:'./'})
})
api.get('/textes/:id', (req, res) =>{
let result = [];
const id = req.params.id;
const text = fs.readFileSync(GetTextData()[`${id}`].path,'UTF-8')
const paraphs = text.split('§');
if(paraphs.length<2){
result = {id, text}
res.json(
result
);
}else{
result = {id, paraphs}
res.json(
result
)
}
})
api.get('/hobbies', (req, res) =>{
res.json(
GetHobbiesData()
)
})
api.get('/assets', (req, res) =>{
res.json(
GetAssetsData()
)
})
api.post('/mail', (req, res) =>{
if(req.body.company === null){
req.body.company ="aucun";
}
if(req.body.subject === null){
req.body.subject="aucun";
}
let mailOptions = {
from: req.body.from,
to: 'phmerle@free.fr',
subject: 'Message reçu de www.logikal.pro',
text : req.body.msg,
html : '<b>De: </b>' + req.body.surname +' '+ req.body.name + '<br>' +
'<b>Société: </b>' + req.body.company + '<br>'+
'<b>Objet : </b>' + req.body.subject + '<br>' +
'<b>Message : </b><br>' + req.body.msg + '<br>' +
'<b>Coordonnées : </b>' + req.body.tel + ' /<br> ' +req.body.email
}
mailer.sendMail(mailOptions, error => console.log(error), info => console.log('Message sent: '+ info.messageId))
mailer.close();
})
app.use('/phm', api); // uniquement des caractères alpha pour l'ancre sinon erreur 'CANNOT GET'
const port = 4201;
const host = "localhost";
app.listen(port, host =>{
console.log(`listening on ${port}`)
})
这是我的文件夹结构: