first commit
commit
502f14336b
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"env": {
|
||||
"node": true,
|
||||
"jest": true,
|
||||
"es6": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"parserOptions": {
|
||||
"ecmaFeatures": {
|
||||
"jsx": true
|
||||
},
|
||||
"ecmaVersion": 2018,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"globals": {
|
||||
"process": true,
|
||||
"__dirname": true
|
||||
},
|
||||
"rules": {
|
||||
"indent": ["error", 2],
|
||||
"linebreak-style": ["error", "unix"],
|
||||
"quotes": ["error", "double"],
|
||||
"semi": ["error", "always"]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
logs
|
||||
*.log
|
||||
.idea
|
||||
build/Release
|
||||
dist
|
||||
node_modules
|
|
@ -0,0 +1,9 @@
|
|||
# NodeJS APIs
|
||||
|
||||
## Quick start
|
||||
|
||||
1. Make sure that you have Node v8 or above installed.
|
||||
2. Clone this repo using `git clone `
|
||||
3. Move to the appropriate directory: `cd {folder}`.
|
||||
4. Run `npm install` in order to install dependencies
|
||||
5. At this point you can run `npm start`.
|
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"name": "node-express-babel-boilerplate",
|
||||
"version": "1.0.0",
|
||||
"description": "Boilerplate of node.js express app with babel and es6 source code.",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"eslint": "eslint --fix src/",
|
||||
"watch": "babel -w src/ -d dist/",
|
||||
"build": "babel --copy-files src/ -d dist/",
|
||||
"prod": "cross-env NODE_ENV=production node dist/index.js",
|
||||
"start": "cross-env NODE_ENV=development env-cmd .env babel --plugins transform-node-env-inline -w src/ -d dist/ | nodemon --watch dist",
|
||||
"postinstall": "npm run build"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "~1.13.2",
|
||||
"cheerio": "^1.0.0-rc.2",
|
||||
"compression": "^1.7.3",
|
||||
"cookie-parser": "~1.3.5",
|
||||
"cross-env": "^5.2.0",
|
||||
"debug": "2.6.9",
|
||||
"dotenv": "^6.0.0",
|
||||
"env-cmd": "^8.0.2",
|
||||
"express": "~4.13.1",
|
||||
"express-fileupload": "^1.0.0",
|
||||
"helmet": "^3.13.0",
|
||||
"jade": "~1.11.0",
|
||||
"lodash": "^4.17.13",
|
||||
"moment": "^2.22.2",
|
||||
"morgan": "1.9.1",
|
||||
"request": "^2.88.0",
|
||||
"serve-favicon": "~2.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel": "^6.5.2",
|
||||
"babel-cli": "^6.16.0",
|
||||
"babel-plugin-transform-node-env-inline": "^0.4.3",
|
||||
"babel-preset-env": "^1.7.0",
|
||||
"babel-preset-es2015": "^6.16.0",
|
||||
"babel-preset-stage-0": "^6.16.0",
|
||||
"babel-preset-stage-2": "^6.24.1",
|
||||
"eslint": "^5.7.0",
|
||||
"nodemon": "^1.11.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export const PORT = 4400;
|
|
@ -0,0 +1,5 @@
|
|||
const homeHandler = (req, res) => {
|
||||
res.send("Home");
|
||||
};
|
||||
|
||||
export default homeHandler;
|
|
@ -0,0 +1,91 @@
|
|||
import "dotenv/config";
|
||||
|
||||
import express from "express";
|
||||
import path from "path";
|
||||
import logger from "morgan";
|
||||
import cookieParser from "cookie-parser";
|
||||
import bodyParser from "body-parser";
|
||||
import helmet from "helmet";
|
||||
import compression from "compression";
|
||||
import fileUpload from "express-fileupload";
|
||||
import routes from "./routes/index";
|
||||
import {PORT} from "./config/constants";
|
||||
|
||||
var app = express();
|
||||
|
||||
app.use(compression()); //Compress all routes
|
||||
app.use(helmet());
|
||||
app.use(fileUpload());
|
||||
|
||||
// view engine setup
|
||||
app.set("views", path.join(__dirname, "views"));
|
||||
app.set("view engine", "jade");
|
||||
|
||||
app.set("port", process.env.PORT || PORT);
|
||||
|
||||
app.use(
|
||||
logger("dev", {
|
||||
skip: function (req) {
|
||||
if (req.url == "/ping") {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({extended: false}));
|
||||
app.use(cookieParser());
|
||||
app.use(express.static(path.join(__dirname, "dist")));
|
||||
|
||||
// Enable CORS
|
||||
app.use(function (req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
|
||||
res.header(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Origin, X-Requested-With, Content-Type, Accept"
|
||||
);
|
||||
next();
|
||||
});
|
||||
|
||||
app.use("/", routes);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
var err = new Error("Not Found");
|
||||
err.status = 404;
|
||||
next(err);
|
||||
});
|
||||
|
||||
// error handlers
|
||||
|
||||
// development error handler
|
||||
// will print stacktrace
|
||||
if (app.get("env") === "development") {
|
||||
app.use(function (err, req, res) {
|
||||
res.status(err.status || 500);
|
||||
res.render("error", {
|
||||
message: err.message,
|
||||
error: err
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// production error handler
|
||||
// no stacktraces leaked to user
|
||||
app.use(function (err, req, res) {
|
||||
res.status(err.status || 500);
|
||||
res.render("error", {
|
||||
message: err.message,
|
||||
error: {}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(process.env.PORT || PORT, () =>
|
||||
console.log(`App listening on http://localhost:${process.env.PORT || PORT}`)
|
||||
);
|
||||
|
||||
export default app;
|
|
@ -0,0 +1,11 @@
|
|||
import express from "express";
|
||||
import Ping from "../handlers/pingTest";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* GET home page. */
|
||||
router.get("/ping", (req, res) => {
|
||||
Ping(req, res);
|
||||
});
|
||||
|
||||
export default router;
|
|
@ -0,0 +1,19 @@
|
|||
export const IsJsonString = str => {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export const invalidCustomParam = (req, res, paramater, message) => {
|
||||
if (!req.query[paramater] || req.query[paramater] === "undefined" || req.query[paramater] === "null") {
|
||||
res.status(400);
|
||||
res.send({
|
||||
message: message ? message : "Invalid paramaters"
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
|
@ -0,0 +1,4 @@
|
|||
block content
|
||||
h1= message
|
||||
h2= error.status
|
||||
pre #{error.stack}
|
Loading…
Reference in New Issue