This commit is contained in:
KLxHunter 2024-09-10 19:12:45 +07:00
parent f384c82ecc
commit 346845fb15
17 changed files with 3563 additions and 2 deletions

1
.env.development Normal file
View File

@ -0,0 +1 @@
PORT=8081

6
.express/init/dotenv.js Normal file
View File

@ -0,0 +1,6 @@
import path from "path"
import dotenv from "dotenv"
import { dirname } from "dirname-filename-esm"
import expressConfig from "../../express.config.js"
dotenv.config({ path: path.join(dirname(import.meta), "../../", `${expressConfig.env[process.env.NODE_ENV]}`) })

3
.express/init/index.js Normal file
View File

@ -0,0 +1,3 @@
import "./dotenv.js"
import "./server.js"
import "./welcome.js"

22
.express/init/server.js Normal file
View File

@ -0,0 +1,22 @@
export const PORT = normalizePort(process.env.PORT || '3000')
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}

8
.express/init/welcome.js Normal file
View File

@ -0,0 +1,8 @@
import { PORT } from "./server.js"
console.log("")
// log node-env
console.log("- NODE_ENV=" + process.env.NODE_ENV)
// log port
console.log("- PORT=" + PORT)
console.log("")

73
.express/www.js Normal file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env node
import "./init/index.js"
/**
* Module dependencies.
*/
import app from '../src/app.js';
import debug from 'debug';
import http from 'http';
import { PORT } from "./init/server.js";
const serverDebug = debug('server:server')
/**
* Get port from environment and store in Express.
*/
app.set('port', PORT);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(PORT);
server.on('error', onError);
server.on('listening', onListening);
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof PORT === 'string'
? 'Pipe ' + PORT
: 'Port ' + PORT;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
serverDebug('Listening on ' + bind);
}

61
.gitignore vendored Normal file
View File

@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next

View File

@ -1,2 +1,27 @@
# express-js-mvc
# Getting Started with Gen Express App
This project was bootstrapped with [Gen Express App](https://github.com/Dalufishe/gen-express-app).
## Available Scripts
In the project directory, you can run:
### `npm run dev`
Runs the app in the development mode.\
Open [http://localhost:8081](http://localhost:8081) to view it in your browser.
The port can be modified in the .env.development file.
This mode includes a hot reloader.
### `npm run start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
The port can be modified in the .env file.
This mode utilizes an optimized build.
## Learn More
You can learn more in the [Gen Express App Github README](https://github.com/Dalufishe/gen-express-app).

6
express.config.js Normal file
View File

@ -0,0 +1,6 @@
export default {
env: {
production: ".env",
development: ".env.development",
},
};

3248
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "gen-express-app dev .express/www.js",
"start": "gen-express-app start .express/www.js"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dirname-filename-esm": "^1.1.1",
"dotenv": "^16.3.1",
"ejs": "~2.6.1",
"express": "~4.16.1",
"hbs": "~4.0.4",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "2.0.0-beta11"
},
"devDependencies": {
"gen-express-app": "^0.2.5",
"nodemon": "^3.0.1"
}
}

18
public/index.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./stylesheets/style.css" />
<title>Express</title>
</head>
<body>
<h1>Express</h1>
<p>
Welcome to Express generated by
<a href="https://github.com/Dalufishe/gen-express-app" target="_blank">
`gen-express-app`</a
>
</p>
</body>
</html>

View File

@ -0,0 +1,9 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00b7ff;
}

22
src/app.js Normal file
View File

@ -0,0 +1,22 @@
import express from 'express';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import { dirname } from "dirname-filename-esm"
import usersRouter from './routers/users.js';
// app
const app = express();
// plugins
app.use(logger(process.env.NODE_ENV === "production" ? "common" : "dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(dirname(import.meta), "../", 'public')));
// routers
app.use('/users', usersRouter);
export default app;

10
src/controllers/users.js Normal file
View File

@ -0,0 +1,10 @@
import usersModel from "../models/users.js";
const userController = {
getUsers: (req, res, next) => {
const users = usersModel.getUsers()
res.send(users);
}
}
export default userController

15
src/models/users.js Normal file
View File

@ -0,0 +1,15 @@
const usersModel = new (class UsersModel {
#users = [
{ name: "tj", title: "the man who created Express.js", github: "https://github.com/tj" },
{ name: "Dalufishe", title: "haha! this is me", github: "https://github.com/Dalufishe" },
]
getUsers() {
return this.#users
}
})()
export default usersModel

8
src/routers/users.js Normal file
View File

@ -0,0 +1,8 @@
import express from 'express';
const router = express.Router();
import userController from "../controllers/users.js";
router.get('/', userController.getUsers);
export default router;