mirror of
https://github.com/wassname/metacar.git
synced 2026-08-28 12:51:58 +08:00
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
var fs = require('fs');
|
|
const express = require('express');
|
|
var path = require("path");
|
|
var cors = require('cors');
|
|
|
|
function fromDir(startPath,filter){
|
|
var files_list = [];
|
|
var files=fs.readdirSync(startPath);
|
|
for(var i=0;i<files.length;i++){
|
|
var filename=path.join(startPath,files[i]);
|
|
var stat = fs.lstatSync(filename);
|
|
if (filename.indexOf(filter)>=0) {
|
|
files_list.push(filename);
|
|
};
|
|
};
|
|
return files_list;
|
|
}
|
|
|
|
function get_path(file){
|
|
return path.join(path.join(__dirname, "webapp/"), file);
|
|
}
|
|
|
|
const app = express();
|
|
app.use(cors());
|
|
|
|
app.use("/dist", express.static(path.join(__dirname, "dist/")));
|
|
app.use("/public", express.static(path.join(__dirname, "webapp/public/")));
|
|
app.use("/docs", express.static(path.join(__dirname, "docs/")));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(get_path("index.html"));
|
|
});
|
|
|
|
/**
|
|
* Create all HTML routes
|
|
* **/
|
|
const files = fromDir(path.join(__dirname, "webapp/"),'.html');
|
|
files.forEach(file => {
|
|
let route = file.split("/");
|
|
route = route[route.length - 1];
|
|
|
|
console.log("Open route:", route, file);
|
|
app.get('/'+route, (req, res) => {
|
|
res.sendFile(file);
|
|
});
|
|
});
|
|
|
|
// Start the server
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => {
|
|
console.log(`App listening on port ${PORT}`);
|
|
console.log('Press Ctrl+C to quit.');
|
|
});
|