Skip to content

Instantly share code, notes, and snippets.

View slmanju's full-sized avatar
💭
Whatever you are be a good one

Manjula Jayawardana slmanju

💭
Whatever you are be a good one
View GitHub Profile
var express = require('express');
var router = express.Router();
var employee = require("../controllers/employee-controller");
// Get all employees
router.get('/', employee.list);
// Get single employee by id
router.get('/show/:id', employee.findById);
var EmployeeController = {};
EmployeeController.list = function(req, res) {
res.send('find all');
};
EmployeeController.findById = function(req, res) {
res.send('find by id');
};
var index = require('./server/routes/index'); // update index route
var employees = require('./server/routes/employees'); // update user route
app.set('views', path.join(__dirname, 'server/views')); // update views
app.use('/employees', employees); // update users to employees
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var employeeSchema = new Schema({
firstName:{ type: String, required: true },
lastName:{ type: String, required: true },
email: { type: String, required: true },
password: String
});
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/nodeems', function (err) {
if (err) throw err;
console.log('Successfully connected to the database');
});
var Employee = require("../models/employee");
var EmployeeController = {};
EmployeeController.list = function(req, res) {
Employee.find({}, function (err, employees) {
if (err) throw err;
res.render("../views/employee/index", {employees: employees});
});
};
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<div class="container">
<h3><a href="/employees/create">Create Employee</a></h3>
<h1>Employee List</h1>
@RestControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(value = { MethodArgumentTypeMismatchException.class })
public ErrorResponse handleBadRequest(MethodArgumentNotValidException exception) {
BindingResult bindingResult = exception.getBindingResult();
List<String> errors = bindingResult.getAllErrors()
.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), exception.getMessage(), errors);
public class UnauthorizedHandler implements AuthenticationEntryPoint {
private ObjectMapper mapper = new ObjectMapper();
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authenticationException)
throws IOException, ServletException {
String authorizationToken = request.getHeader(HttpHeaders.AUTHORIZATION);
protected void configure(HttpSecurity http) throws Exception {
// disabling csrf since this is REST
http.csrf().disable();
// handle unauthorized access
http.exceptionHandling().authenticationEntryPoint(new UnauthorizedHandler());
// no need to have a user session
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);