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