code convention
This commit is contained in:
+10
-7
@@ -45,19 +45,22 @@ module.exports = (config) => {
|
||||
|
||||
const showError = (resPath, code, res) => {
|
||||
const errorPath = path.join(config['data_dir'],config['home']['error']);
|
||||
if (fs.existsSync(errorPath))
|
||||
render(res, errorPath, {error: code, path: resPath}, code);
|
||||
else
|
||||
fs.access(errorPath, (err) => {
|
||||
if(err)
|
||||
res.sendStatus(code);
|
||||
else
|
||||
render(res, errorPath, {error: code, path: resPath}, code);
|
||||
});
|
||||
};
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
const homePath = `${config['data_dir']}/${config['home']['index']}`;
|
||||
if (fs.existsSync(homePath))
|
||||
render(res, homePath, {articles: articles});
|
||||
else {
|
||||
fs.access(homePath,(err)=>{
|
||||
if(err)
|
||||
showError(req.path, 404, res);
|
||||
}
|
||||
else
|
||||
render(res, homePath, {articles: articles});
|
||||
});
|
||||
});
|
||||
|
||||
app.get('*', express.static(config['data_dir']));
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ const merge = (ref, src) => {
|
||||
return ref;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key]));
|
||||
Object.keys(ref).forEach((key) =>out[key] = merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
return src;
|
||||
|
||||
+6
-6
@@ -54,16 +54,16 @@ module.exports = (config) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
const paths = fileList
|
||||
.map(path => path.substr(config['data_dir'].length))
|
||||
.filter(path => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
||||
.map(path => path.substr(0, path.length - config['article']['index'].length))
|
||||
.map(path => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||
.filter(matches => matches && matches.length > 1);
|
||||
.map((path) =>path.substr(config['data_dir'].length))
|
||||
.filter((path) =>path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
||||
.map((path) =>path.substr(0, path.length - config['article']['index'].length))
|
||||
.map((path) =>path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||
.filter((matches) =>matches && matches.length > 1);
|
||||
if (paths.length === 0)
|
||||
cb(null, []);
|
||||
const list = [];
|
||||
let remaining = 0;
|
||||
paths.forEach(matches => {
|
||||
paths.forEach((matches) =>{
|
||||
const article = {
|
||||
path: path.join(config['data_dir'], matches[1], matches[2], matches[3], config['article']['index']),
|
||||
parent: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ if (!fs.existsSync('data')) {
|
||||
|
||||
copy(path.join('sample_data','home'), 'data');
|
||||
|
||||
const pad0 = n => ('0' + n).substr(-2);
|
||||
const pad0 = (n) =>('0' + n).substr(-2);
|
||||
|
||||
const datetime = new Date();
|
||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||
|
||||
+18
-18
@@ -32,23 +32,23 @@ afterAll(() => {
|
||||
});
|
||||
|
||||
describe('Test root path', () => {
|
||||
test('404 no index no error', done => {
|
||||
request(app).get('/').then(response => {
|
||||
test('404 no index no error', (done) =>{
|
||||
request(app).get('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('404 no index but error page', done => {
|
||||
test('404 no index but error page', (done) =>{
|
||||
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
|
||||
request(app).get('/').then(response => {
|
||||
request(app).get('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(404);
|
||||
expect(response.text).toBe('error 404 at /');
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('200 index page', done => {
|
||||
test('200 index page', (done) =>{
|
||||
fs.writeFileSync(path.join(dataDir,testIndex), 'hello there');
|
||||
request(app).get('/').then(response => {
|
||||
request(app).get('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.text).toBe('hello there');
|
||||
done();
|
||||
@@ -58,23 +58,23 @@ describe('Test root path', () => {
|
||||
});
|
||||
|
||||
describe('Test static files', () => {
|
||||
test('404 invalid file no error page', done => {
|
||||
request(app).get('/somefile.txt').then(response => {
|
||||
test('404 invalid file no error page', (done) =>{
|
||||
request(app).get('/somefile.txt').then((response) =>{
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('404 invalid file but error page', done => {
|
||||
test('404 invalid file but error page', (done) =>{
|
||||
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
|
||||
request(app).get('/somefile.txt').then(response => {
|
||||
request(app).get('/somefile.txt').then((response) =>{
|
||||
expect(response.statusCode).toBe(404);
|
||||
expect(response.text).toBe('error 404 at /somefile.txt');
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('200 valid file', done => {
|
||||
test('200 valid file', (done) =>{
|
||||
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
||||
request(app).get('/somefile.txt').then(response => {
|
||||
request(app).get('/somefile.txt').then((response) =>{
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.text).toBe('filecontent');
|
||||
done();
|
||||
@@ -83,20 +83,20 @@ describe('Test static files', () => {
|
||||
});
|
||||
|
||||
describe('Test other requests', () => {
|
||||
test('400 POST', done => {
|
||||
request(app).post('/').then(response => {
|
||||
test('400 POST', (done) =>{
|
||||
request(app).post('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('400 PUT', done => {
|
||||
request(app).put('/').then(response => {
|
||||
test('400 PUT', (done) =>{
|
||||
request(app).put('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
test('400 DELETE', done => {
|
||||
request(app).delete('/').then(response => {
|
||||
test('400 DELETE', (done) =>{
|
||||
request(app).delete('/').then((response) =>{
|
||||
expect(response.statusCode).toBe(400);
|
||||
done();
|
||||
});
|
||||
|
||||
+2
-2
@@ -15,6 +15,6 @@ const deleteFolderSync = (dir) => {
|
||||
|
||||
module.exports = {
|
||||
deleteFolderSync: deleteFolderSync,
|
||||
createEmptyDirs: list => list.forEach(path => fs.mkdirSync(path, {recursive: true})),
|
||||
createEmptyFiles: list => list.forEach(file => fs.writeFileSync(file, '')),
|
||||
createEmptyDirs: (list) =>list.forEach((path) =>fs.mkdirSync(path, {recursive: true})),
|
||||
createEmptyFiles: (list) =>list.forEach((file) =>fs.writeFileSync(file, '')),
|
||||
};
|
||||
Reference in New Issue
Block a user