初始化项目

This commit is contained in:
huangge1199 2024-08-05 16:31:22 +08:00
commit 8a6e23577c
25 changed files with 439 additions and 0 deletions

3
.eslintrc Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "think"
}

37
.gitignore vendored Normal file
View File

@ -0,0 +1,37 @@
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage/
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules/
# IDE config
.idea
# output
output/
output.tar.gz
runtime/
app/
config.development.js
adapter.development.js

22
README.md Normal file
View File

@ -0,0 +1,22 @@
Application created by [ThinkJS](http://www.thinkjs.org)
## Install dependencies
```
npm install
```
## Start server
```
npm start
```
## Deploy with pm2
Use pm2 to deploy app on production enviroment.
```
pm2 startOrReload pm2.json
```

18
development.js Normal file
View File

@ -0,0 +1,18 @@
const path = require('path');
const Application = require('thinkjs');
const watcher = require('think-watcher');
const babel = require('think-babel');
const notifier = require('node-notifier');
const instance = new Application({
ROOT_PATH: __dirname,
APP_PATH: path.join(__dirname, 'app'),
watcher: watcher,
transpiler: [babel, {
presets: ['think-node']
}],
notifier: notifier.notify.bind(notifier),
env: 'development'
});
instance.run();

30
nginx.conf Normal file
View File

@ -0,0 +1,30 @@
server {
listen 80;
server_name example.com www.example.com;
root D:\project\node-server\news-server;
set $node_port 8360;
index index.js index.html index.htm;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:$node_port$request_uri;
proxy_redirect off;
}
location ~ /static/ {
etag on;
expires max;
}
}

55
package.json Normal file
View File

@ -0,0 +1,55 @@
{
"name": "news-server",
"description": "application created by thinkjs",
"version": "1.0.0",
"author": "huangge1199 <huangge1199@hotmail.com>",
"scripts": {
"start": "node development.js",
"test": "THINK_UNIT_TEST=1 nyc ava test/ && nyc report --reporter=html",
"compile": "babel --no-babelrc src/ --presets think-node --out-dir app/",
"lint": "eslint src/",
"lint-fix": "eslint --fix src/"
},
"dependencies": {
"think-cache": "^1.0.0",
"think-session": "^1.0.0",
"think-cache-file": "^1.0.8",
"think-model": "^1.0.0",
"think-session-file": "^1.0.5",
"think-view": "^1.0.0",
"think-view-nunjucks": "^1.0.1",
"think-model-mysql": "^1.0.0",
"think-logger3": "^1.0.0",
"thinkjs": "^3.0.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-think-node": "^1.0.0",
"node-notifier": "^5.0.2",
"think-inspect": "0.0.2",
"think-babel": "^1.0.3",
"think-watcher": "^3.0.0",
"eslint": "^4.2.0",
"eslint-config-think": "^1.0.0",
"ava": "^0.18.0",
"nyc": "^7.0.0"
},
"repository": "",
"license": "MIT",
"engines": {
"node": ">=6.0.0"
},
"readmeFilename": "README.md",
"thinkjs": {
"metadata": {
"name": "news-server",
"description": "application created by thinkjs",
"author": "huangge1199 <huangge1199@hotmail.com>",
"babel": true
},
"projectName": "news-server",
"template": "D:\\Program Files\\nodejs\\node_global\\node_modules\\think-cli\\default_template",
"clone": false,
"isMultiModule": false
}
}

15
pm2.json Normal file
View File

@ -0,0 +1,15 @@
{
"apps": [{
"name": "news-server",
"script": "production.js",
"cwd": "D:\project\node-server\news-server",
"exec_mode": "fork",
"max_memory_restart": "1G",
"autorestart": true,
"node_args": [],
"args": [],
"env": {
}
}]
}

11
production.js Normal file
View File

@ -0,0 +1,11 @@
const path = require('path');
const Application = require('thinkjs');
const instance = new Application({
ROOT_PATH: __dirname,
APP_PATH: path.join(__dirname, 'app'),
proxy: true, // use proxy
env: 'production'
});
instance.run();

1
src/bootstrap/master.js Normal file
View File

@ -0,0 +1 @@
// invoked in master

1
src/bootstrap/worker.js Normal file
View File

@ -0,0 +1 @@
// invoked in worker

109
src/config/adapter.js Normal file
View File

@ -0,0 +1,109 @@
const fileCache = require('think-cache-file');
const nunjucks = require('think-view-nunjucks');
const fileSession = require('think-session-file');
const mysql = require('think-model-mysql');
const {Console, File, DateFile} = require('think-logger3');
const path = require('path');
const isDev = think.env === 'development';
/**
* cache adapter config
* @type {Object}
*/
exports.cache = {
type: 'file',
common: {
timeout: 24 * 60 * 60 * 1000 // millisecond
},
file: {
handle: fileCache,
cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
pathDepth: 1,
gcInterval: 24 * 60 * 60 * 1000 // gc interval
}
};
/**
* model adapter config
* @type {Object}
*/
exports.model = {
type: 'mysql',
common: {
logConnect: isDev,
logSql: isDev,
logger: msg => think.logger.info(msg)
},
mysql: {
handle: mysql,
database: '',
prefix: '',
encoding: 'utf8',
host: '',
port: '',
user: '',
password: '',
dateStrings: true
}
};
/**
* session adapter config
* @type {Object}
*/
exports.session = {
type: 'file',
common: {
cookie: {
name: 'thinkjs'
// keys: ['werwer', 'werwer'],
// signed: true
}
},
file: {
handle: fileSession,
sessionPath: path.join(think.ROOT_PATH, 'runtime/session')
}
};
/**
* view adapter config
* @type {Object}
*/
exports.view = {
type: 'nunjucks',
common: {
viewPath: path.join(think.ROOT_PATH, 'view'),
sep: '_',
extname: '.html'
},
nunjucks: {
handle: nunjucks
}
};
/**
* logger adapter config
* @type {Object}
*/
exports.logger = {
type: isDev ? 'console' : 'dateFile',
console: {
handle: Console
},
file: {
handle: File,
backups: 10, // max chunk number
absolute: true,
maxLogSize: 50 * 1024, // 50M
filename: path.join(think.ROOT_PATH, 'logs/app.log')
},
dateFile: {
handle: DateFile,
level: 'ALL',
absolute: true,
pattern: '-yyyy-MM-dd',
alwaysIncludePattern: true,
filename: path.join(think.ROOT_PATH, 'logs/app.log')
}
};

4
src/config/config.js Normal file
View File

@ -0,0 +1,4 @@
// default config
module.exports = {
workers: 1
};

View File

@ -0,0 +1,4 @@
// production config, it will load in production enviroment
module.exports = {
workers: 0
};

11
src/config/extend.js Normal file
View File

@ -0,0 +1,11 @@
const view = require('think-view');
const model = require('think-model');
const cache = require('think-cache');
const session = require('think-session');
module.exports = [
view, // make application support view
model(think.app),
cache,
session
];

40
src/config/middleware.js Normal file
View File

@ -0,0 +1,40 @@
const path = require('path');
const isDev = think.env === 'development';
module.exports = [
{
handle: 'meta',
options: {
logRequest: isDev,
sendResponseTime: isDev
}
},
{
handle: 'resource',
enable: isDev,
options: {
root: path.join(think.ROOT_PATH, 'www'),
publicPath: /^\/(static|favicon\.ico)/
}
},
{
handle: 'trace',
enable: !think.isCli,
options: {
debug: isDev
}
},
{
handle: 'payload',
options: {
keepExtensions: true,
limit: '5mb'
}
},
{
handle: 'router',
options: {}
},
'logic',
'controller'
];

3
src/config/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = [
];

5
src/controller/base.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = class extends think.Controller {
__before() {
}
};

7
src/controller/index.js Normal file
View File

@ -0,0 +1,7 @@
const Base = require('./base.js');
module.exports = class extends Base {
indexAction() {
return this.display();
}
};

5
src/logic/index.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = class extends think.Logic {
indexAction() {
}
};

3
src/model/index.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = class extends think.Model {
};

7
test/index.js Normal file
View File

@ -0,0 +1,7 @@
const test = require('ava');
const path = require('path');
require(path.join(process.cwd(), 'production.js'));
test('first test', t => {
const indexModel = think.model('index');
})

48
view/index_index.html Normal file
View File

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>New ThinkJS Application</title>
<style>
*{padding: 0;margin: 0;font-size: 16px;line-height: 20px;font-family: arial;}
a, a:visited{color:#337ab7;text-decoration: none;}
header{padding: 70px 0 70px 0;background-color: #4A6495}
h1{font-size: 36px;color:#fff;font-weight: normal;}
code{ padding: 2px 4px;font-size: 90%;color: #c7254e;background-color: #f9f2f4;border-radius: 4px;}
.content{width: 1000px;margin: auto}
.wrap{width: 1000px;margin: auto}
.content{margin-top: 80px;}
.list{width: 800px;}
.list .item{position: relative;padding-left: 70px;margin-top: 50px;}
.list .item .step{position: absolute;width: 36px;height: 36px;top:-3px;left:0;border: 5px solid #4A6495;border-radius: 23px;text-align: center;line-height: 36px;}
.list .item h2{font-size: 24px;font-weight: normal;}
.list .item p{line-height: 30px;margin-top: 10px}
</style>
</head>
<body>
<header>
<div class="wrap">
<h1>A New App Created By ThinkJS</h1>
</div>
</header>
<div class="content">
<div class="list">
<div class="item">
<div class="step">1</div>
<h2>Generate Files</h2>
<p>Run <code>thinkjs</code> command to create module, controler, model, service and so on. </p>
</div>
<div class="item">
<div class="step">2</div>
<h2>Documentation</h2>
<p>ThinkJS has online html documents. visit <a href="https://thinkjs.org/doc.html">https://thinkjs.org/doc.html</a>.</p>
</div>
<div class="item">
<div class="step">3</div>
<h2>GitHub</h2>
<p>If you have some questions, please <a href="https://github.com/thinkjs/thinkjs/issues">new a issue</a>.</p>
</div>
</div>
</div>
</body>
</html>

0
www/static/css/.gitkeep Normal file
View File

View File

0
www/static/js/.gitkeep Normal file
View File