create and config

master
shoaibalmasi 4 years ago
parent 9394cd23de
commit d6c4ba39cf
  1. 2
      .gitignore
  2. 41
      app.js
  3. 90
      bin/www
  4. 23
      config/config.json
  5. 66
      migrations/20210314175509-create-user.js
  6. 37
      models/index.js
  7. 36
      models/user.js
  8. 18
      package.json
  9. 8
      public/stylesheets/style.css
  10. 9
      routes/index.js
  11. 50
      routes/users.js
  12. 22
      services/user.js
  13. 3
      views/error.ejs
  14. 11
      views/index.ejs

2
.gitignore vendored

@ -0,0 +1,2 @@
node_modules/
package-lock.json

@ -0,0 +1,41 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('word-template:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
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);
/**
* 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;
}
/**
* 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;
debug('Listening on ' + bind);
}

@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "shoaib",
"database": "word_db",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}

@ -0,0 +1,66 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
fatherName: {
type: Sequelize.STRING
},
codeMelli: {
type: Sequelize.INTEGER
},
workUnit: {
type: Sequelize.STRING
},
workUnitCode: {
type: Sequelize.INTEGER
},
address: {
type: Sequelize.TEXT
},
obligationNumber: {
type: Sequelize.INTEGER
},
obligationDate: {
type: Sequelize.DATE
},
requestNumber: {
type: Sequelize.INTEGER
},
requestDate: {
type: Sequelize.DATE
},
writtenOrderNumber: {
type: Sequelize.INTEGER
},
writtenOrderDate: {
type: Sequelize.DATE
},
deadline: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
}
};

@ -0,0 +1,37 @@
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

@ -0,0 +1,36 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
User.init({
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
fatherName: DataTypes.STRING,
codeMelli: DataTypes.INTEGER,
workUnit: DataTypes.STRING,
workUnitCode: DataTypes.INTEGER,
address: DataTypes.TEXT,
obligationNumber: DataTypes.INTEGER,
obligationDate: DataTypes.DATE,
requestNumber: DataTypes.INTEGER,
requestDate: DataTypes.DATE,
writtenOrderNumber: DataTypes.INTEGER,
writtenOrderDate: DataTypes.DATE,
deadline: DataTypes.INTEGER
}, {
sequelize,
modelName: 'User',
});
return User;
};

@ -0,0 +1,18 @@
{
"name": "word-template",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "~2.6.1",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pg": "^8.5.1",
"sequelize": "^6.5.0"
}
}

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

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;

@ -0,0 +1,50 @@
var express = require('express');
var router = express.Router();
const userServeice = require('../services/user')
/* GET users listing. */
router.post('/add', function(req, res, next) {
console.log('hereeeeeeeeee');
const userInfo ={
firstName : req.body.firstName,
lastName : req.body.lastName,
fatherName : req.body.fatherName,
codeMelli : req.body.codeMelli,
workUnit : req.body.workUnit,
workUnitCode : req.body.workUnitCode,
address : req.body.address,
obligationNumber : req.body.obligationNumber,
obligationDate : req.body.obligationDate,
requestNumber : req.body.requestNumber,
requestDate : req.body.requestDate,
writtenOrderNumber : req.body.writtenOrderNumber,
writtenOrderDate : req.body.writtenOrderDate,
deadline : req.body.deadline
}
userServeice.addUser(userInfo)
.then((result) => {
res.status(200).send({
success: true,
user: result
})
})
.catch(err => {
if (err.eText) {
if (typeof err.eText !== 'string') {
err.eText = err.eText.toString()
}
res.status(err.eCode).send({
success: false,
error: err.eText
})
} else {
res.status(500).send({
success: false,
error: err.toString()
})
}
})
});
module.exports = router;

@ -0,0 +1,22 @@
let models = require('../models')
let methods = {}
methods.addUser = (userInfo) => {
return new Promise((resolve, reject) => {
models.User.create(userInfo).then((user) => {
console.log(user);
resolve(user)
}).catch(err => {
reject(err)
})
})
}
module.exports = methods

@ -0,0 +1,3 @@
<h1><%= message %></h1>
<h2><%= error.status %></h2>
<pre><%= error.stack %></pre>

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
</body>
</html>
Loading…
Cancel
Save