first
This commit is contained in:
		
							
								
								
									
										35
									
								
								server/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								server/index.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
const express = require('express');
 | 
			
		||||
const pug = require('pug');
 | 
			
		||||
 | 
			
		||||
const route = require('./route');
 | 
			
		||||
const { STATIC_PORT } = require('../gulpfile.js/config/server');
 | 
			
		||||
const { srcView, output } = require('../gulpfile.js/config/directories');
 | 
			
		||||
const pugOptions = require('../gulpfile.js/config/pug');
 | 
			
		||||
const { renderErrorHTML } = require('../gulpfile.js/utils');
 | 
			
		||||
 | 
			
		||||
const app = express();
 | 
			
		||||
app.locals.moment = require('moment');
 | 
			
		||||
const { log } = console;
 | 
			
		||||
 | 
			
		||||
app.engine('pug', (path, options, callback) => {
 | 
			
		||||
  const opts = { ...pugOptions, ...options };
 | 
			
		||||
 | 
			
		||||
  pug.renderFile(path, opts, (err, result) => {
 | 
			
		||||
    const data = result || renderErrorHTML(err.message);
 | 
			
		||||
 | 
			
		||||
    callback(null, data);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
app.set('views', srcView);
 | 
			
		||||
app.set('view engine', 'pug');
 | 
			
		||||
app.use(express.static(output));
 | 
			
		||||
app.use(express.json());
 | 
			
		||||
app.use(express.urlencoded({
 | 
			
		||||
  extended: true,
 | 
			
		||||
}));
 | 
			
		||||
app.use('/', route);
 | 
			
		||||
 | 
			
		||||
const logPort = `----- View server is running at http://localhost:${STATIC_PORT} -----`;
 | 
			
		||||
 | 
			
		||||
app.listen(STATIC_PORT, () => log(logPort));
 | 
			
		||||
							
								
								
									
										83
									
								
								server/route.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								server/route.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
/* eslint-disable-next-line */
 | 
			
		||||
const { join } = require('path');
 | 
			
		||||
const express = require('express');
 | 
			
		||||
 | 
			
		||||
const router = express.Router();
 | 
			
		||||
const { output, srcLocales } = require('../gulpfile.js/config/directories');
 | 
			
		||||
const { renderErrorHTML } = require('../gulpfile.js/utils');
 | 
			
		||||
const { DEFAULT_LANG } = require('../gulpfile.js/config/server');
 | 
			
		||||
 | 
			
		||||
const multiLang = process.env.MULTI_LANGUAGE;
 | 
			
		||||
 | 
			
		||||
function forceRequire(path) {
 | 
			
		||||
  const realPath = join(__dirname, path);
 | 
			
		||||
 | 
			
		||||
  delete require.cache[realPath];
 | 
			
		||||
 | 
			
		||||
  /* eslint-disable-next-line */
 | 
			
		||||
  return require(path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
router.get('/', (_, res) => {
 | 
			
		||||
  const defautLangPath = multiLang ? `/${DEFAULT_LANG}` : '';
 | 
			
		||||
 | 
			
		||||
  res.redirect(`${defautLangPath}/index.html`);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/*.html', (req, res) => {
 | 
			
		||||
  try {
 | 
			
		||||
    let lang;
 | 
			
		||||
    let match;
 | 
			
		||||
    let localeLang;
 | 
			
		||||
    let { path: url } = req;
 | 
			
		||||
 | 
			
		||||
    if (multiLang) {
 | 
			
		||||
      const testLang = /^\/([^/]+)\//.exec(url);
 | 
			
		||||
 | 
			
		||||
      if (!testLang) {
 | 
			
		||||
        throw new Error('No language in the url');
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      [match, lang] = testLang;
 | 
			
		||||
      localeLang = forceRequire(`../${srcLocales + lang}.json`);
 | 
			
		||||
 | 
			
		||||
      url = url.replace(match, '');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const testFile = /[/]?(.+)\.html/.exec(url);
 | 
			
		||||
 | 
			
		||||
    if (!testFile) {
 | 
			
		||||
      throw new Error('Not found');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res.render(testFile[1], {
 | 
			
		||||
      $translator: localeLang || {},
 | 
			
		||||
      $localeName: lang,
 | 
			
		||||
      $path: req.url,
 | 
			
		||||
    }, (err, html) => {
 | 
			
		||||
      if (err) {
 | 
			
		||||
        throw err;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      res.send(html);
 | 
			
		||||
    });
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    res.send(renderErrorHTML(err)).status(404);
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get(/^\/.*[^(.html)]$/, (req, res) => {
 | 
			
		||||
  res.redirect(join(req.path, 'index.html'));
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.post('*', (req, res) => {
 | 
			
		||||
  try {
 | 
			
		||||
    const json = forceRequire(join(__dirname, '..', output, req.url));
 | 
			
		||||
 | 
			
		||||
    res.send(json);
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    res.send(renderErrorHTML(err)).status(404);
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
module.exports = router;
 | 
			
		||||
		Reference in New Issue
	
	Block a user