Skip to content

Instantly share code, notes, and snippets.

View bewarusman's full-sized avatar
🎯
Focusing

Bewar Salah bewarusman

🎯
Focusing
View GitHub Profile
componentDidMount() {
this.$el = $(this.el);
this.currentTable = this.$el.DataTable({});
}
import React from "react";
class Datatable extends React.Component {
render() {
return (
<table ref={(el) => (this.el = el)}>
</table>
);
}
}
// This is a basic class component which only displays message in h1 tag.
import React from "react";
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
// npm install -S express jsonwebtoken body-parser bcrypt mongoose
// npm install -D nodemon
// adding "start": "nodemon" to start script
// npm start
// CONSTANTS
const PORT = 3000;
const SECRET_KEY = "a_secret_key_to_sign_jwt";
const SALT_ROUNDS = 10;
// import packages
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const path = require("path");
const fs = require("fs");
// create and configure express app
const app = express();
app.use(bodyParser.json()); //parsing json files
@bewarusman
bewarusman / one-file.js
Last active March 31, 2023 07:47
Minimal Express.js Server
/** STEP 1: Packages */
// For a minimal node.js server we need to install and import the bellow packages
const express = require("express"); // fast, open-source node.js server
const cors = require("cors"); // enables CORS (cross-origin resource sharing)
const bodyParser = require("body-parser"); // parses json body into javascript object
const morgan = require("morgan"); // log http requests
const mongoose = require("mongoose"); // mongodb orm
/** STEP 2: DATABASE */
var createTodo = async (req, res, next) => {
try {
var text = req.body.text;
if(!text) {
var err = new Error('todo should be provided');
err.msg = 'todo should be provided';
err.status = 406;
next(err);
return;
}
const chai = require('chai');
const sinon = require('sinon');
const expect = chai.expect;
const {createTodo, listTodos} = require('./todoController');
describe('todoController', function() {
beforeEach(function() {
req = {body: {text: 'Go to the meeting!'}, db: {TodoModel: {create: null, findAll: null}}};
res = {json: sinon.fake()};
it('text should be provided', async function() {
req.body.text = null;
await createTodo(req, res, next);
expect(next.args[0][0]).to.have.property('msg', 'todo should be provided');
});
describe('todoController', function() {
describe('createTodo', function() {
it('text should be provided');
it('should send success result if todo is saved to database');
it('should send fail result if todo is not saved to database');
});
describe('fetchTodos', function() {
it('should send a list of todos with success message');
it('should send fail result if todo is not fetched from database');