express-boilerplate-gen
Version:
Generates a default express project
57 lines (46 loc) • 1.46 kB
JavaScript
const fs = require('fs');
const path = require('path');
const args = process.argv;
var html = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Title</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>`;
var server = `const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + 'public/index.html');
});
app.listen({{port}}, () => console.log('server started'));`;
var root = '/';
var port = 3000;
var public = 'public';
var app = 'app.js';
if (args.includes('-p')) {
port = parseInt(args[args.indexOf('-p') + 1]);
}
if (args.includes('-n')) {
app = args[args.indexOf('-n') + 1];
}
if (args.includes('-d')) {
public = args[args.indexOf('-d') + 1];
}
server = server.replace('{{port}}', port);
fs.writeFileSync(app, server, 'utf8');
fs.mkdirSync('public');
fs.writeFileSync('public/index.html', html, 'utf8');
fs.writeFileSync('public/script.js', '', 'utf8');
fs.writeFileSync('public/style.css', '', 'utf8');