# fonttech_base_server

> This is a core base project for Font Tech server projects using MongoDB and Redis.

Latest version **1.0.101** (published 2023-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install fonttech_base_server
pnpm add fonttech_base_server
yarn add fonttech_base_server
bun add fonttech_base_server
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.101 |
| Published | 2023-07-10 |
| First published | 2023-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 32 |
| Unpacked size | 830.4 KB |
| Known vulnerabilities | 0 (+44 in 8 direct dependencies) |
| Install scripts | no |
| Author | Leandro by FontTech |
| Maintainers | fontalland |
| Keywords | font, font tech, javascript, api, node, express, mongo, mongodb, jwt, postman, i18n, jwt-authentication, token, eslint, starter, web, app, rest, skeleton, async, await, mvp, front-end, testing, prettier, mocha, chai, redis, JSDoc |

## Links

- npm: https://www.npmjs.com/package/fonttech_base_server
- Repository: https://github.com/FontTech/FontBaseServer
- Homepage: https://github.com/FontTech/FontBaseServer#readme
- Issues: https://github.com/FontTech/FontBaseServer/issues
- npm.io page: https://npm.io/package/fonttech_base_server

## Dependencies (32)

- [ejs](https://npm.io/package/ejs.md) ^3.0.1
- [cors](https://npm.io/package/cors.md) ^2.8.5
- [i18n](https://npm.io/package/i18n.md) ^0.8.5
- [uuid](https://npm.io/package/uuid.md) ^3.4.0
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [dotenv](https://npm.io/package/dotenv.md) latest
- [helmet](https://npm.io/package/helmet.md) ^3.21.3
- [moment](https://npm.io/package/moment.md) ^2.24.0
- [morgan](https://npm.io/package/morgan.md) ^1.9.1
- [multer](https://npm.io/package/multer.md) ^1.4.5-lts.1
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.704.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [date-fns](https://npm.io/package/date-fns.md) ^2.10.0
- [passport](https://npm.io/package/passport.md) ^0.4.1
- [validator](https://npm.io/package/validator.md) ^12.2.0
- [nodemailer](https://npm.io/package/nodemailer.md) ^6.4.4
- [remark-cli](https://npm.io/package/remark-cli.md) ^7.0.1
- [request-ip](https://npm.io/package/request-ip.md) ^2.1.3
- [underscore](https://npm.io/package/underscore.md) ^1.10.2
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [compression](https://npm.io/package/compression.md) ^1.7.4
- [dotenv-safe](https://npm.io/package/dotenv-safe.md) ^8.2.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.0
- [trim-request](https://npm.io/package/trim-request.md) ^1.0.6
- [bcrypt-nodejs](https://npm.io/package/bcrypt-nodejs.md) 0.0.3
- [elasticsearch](https://npm.io/package/elasticsearch.md) ^16.7.1
- [redis-promisify](https://npm.io/package/redis-promisify.md) ^0.1.2
- [express-validator](https://npm.io/package/express-validator.md) ^6.4.0
- [express-expeditious](https://npm.io/package/express-expeditious.md) ^5.1.1
- [expeditious-engine-redis](https://npm.io/package/expeditious-engine-redis.md) ^0.1.2
- [nodemailer-mailgun-transport](https://npm.io/package/nodemailer-mailgun-transport.md) ^2.0.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 1.0.101 (latest) — 2023-07-10
- 1.0.1 — 2023-07-10
- 1.0.0 — 2023-07-10

## README

# Font Tech - Basic nodejs project skeleton
     ______    ____    _   _   _______     _                   _
    |  ____|  / __ \  | \ | | |__   __|   | |                 | |
    | |__    | |  | | |  \| |    | |      | |_    ___    ___  | |__
    |  __|   | |  | | | . ` |    | |      | __|  / _ \  / __| | '_ \
    | |      | |__| | | |\  |    | |      | |_  |  __/ | (__  | | | |
    |_|       \____/  |_| \_|    |_|       \__|  \___|  \___| |_| |_|


## Getting started

This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)

This project is created to help other developers create a **basic REST API in an easy way with Node.js**. This basic example shows how powerful and simple JavaScript can be. Do you want to contribute? Pull requests are always welcome to show more features.

## Features

-   Multiple environment ready (development, production)
-   Custom email/password user system with basic security and blocking for preventing brute force attacks.
-   Compressed responses.
-   Secured HTTP headers.
-   CORS ready.
-   Cache ready (Redis).
-   HTTP request logger in development mode.
-   i18n ready (for sending emails in multiple languages).
-   User roles.
-   Pagination ready.
-   User profile.
-   Users list for admin area.
-   Cities model and controller example.
-   Login access log with IP, browser and country location (for country it looks for the header `cf-ipcountry` that CloudFlare creates when protecting your website).
-   API autogenerated documentation by Postman.
-   API collection example for Postman.
-   Testing with mocha/chai for API endpoints.
-   NPM scripts for cleaning and seeding the MongoDB database.
-   NPM script for keeping good source code formatting using prettier and ESLint.
-   Use of ESLint for good coding practices.
-   Mailer example with Nodemailer and Mailgun.
-   Ability to refresh token
-   JWT Tokens, make requests with a token after login with `Authorization` header with value `Bearer yourToken` where `yourToken` is the **signed and encrypted token** given in the response from the login process.

## Requirements

-   Node.js **8+**
-   MongoDB **3.6+**
-   Redis **5.0+**

### Login credentials

email: `admin@admin.com`
password: `12345`

## How to install

### Using Git (recommended)

1.  Clone the project from github. Change "myproject" to your project name.

```bash
git clone https://gitlab.com/fontalland/fontbaseserver.git ./fontbaseserver
```

### Using manual download ZIP

1.  Download repository
2.  Uncompress to your desired directory

### Install npm dependencies after installing (Git or manual download)

```bash
cd myproject
npm install
npm update
```

### Setting up environments (development or production)

1.  In the root this repository you will find a file named `.env.example`
2.  Create a new file by copying and pasting the file and then renaming it to just `.env`
3.  The file `.env` is already ignored, so you never commit your credentials.
4.  Change the values of the file to your environment (development or production)
5.  Upload the `.env` to your environment server(development or production)
6.  If you use the postman collection to try the endpoints, change value of the variable `server` on your environment to the url of your server, for development mode use <http://localhost:3000>

**IMPORTANT:** By default token expires in 3 days (4320 minutes set in .env.example). You can refresh token at endpoint GET /token. If everything it´s ok you will get a new token.

### Mailer

To ensure the deliverability of emails sent by this API, `Mailgun` is used for mailing users when they sign up, so if you want to use that feature go sign up at their website <https://www.mailgun.com>

If you want to try a different method it´s ok, I used <https://nodemailer.com> for this API and they have different transport methods like: smtp.

### i18n

Language is automatically detected from `Accept-Language` header on the request. So either you send locale manually on the request or your browser will send its default, if `Accept-Language` header is not sent then it will use `en` locale as default.

## How to run

### Database cleaning and seeding samples

There are 3 available commands for this: `fresh`, `clean` and `seed`.

```bash
npm run command
```

-   `fresh` cleans and then seeds the database with dynamic data.
-   `clean` cleans the database.
-   `seed` seeds the database with dynamic data.

### Running in development mode (lifting API server)

```bash
npm run dev
```

You will know server is running by checking the output of the command `npm run dev`

```bash
****************************
*    Starting Server
*    Port: 3000
*    NODE_ENV: development
*    Database: MongoDB
*    DB Connection: OK
****************************
```

### Running tests

It´s a good practice to do tests at your code, so a sample of how to do that in `mocha/chai` is also included in the `/test` directory

```bash
npm run test
```

### Formatting code

Format your code with prettier by typing:

```bash
npm run format
```

### Formatting markdown files

Format all your markdown files with remark by typing:

```bash
npm run remark
```

### Linting code

Lint your code with ESLint by typing:

```bash
npm run lint
```

## Usage

Once everything is set up to test API routes either use Postman or any other api testing application. Default username/password combination for login is `admin@admin.com/12345`.

### API documentation

<https://documenter.getpostman.com/view/487539/RWaHwoLV>

### Postman API example collection

You can import the example collection to Postman. To import, click the import button located and select `postman-example.json` located within the root directory.

Go to `manage environments` to create environments for development, production, etc. On each of the environments you create you will need to:

1.  Create a new key `authToken` and within the `/login` request this value is automatically updated after a successfull login through a script located in the `tests` tab. Each time you make a request to the API it will send `Authorization` header with the `token` value in the request, you can check this on the headers of users or cities endpoints in the Postman example.

2.  Create a second key `server` with the url of your server, for development mode use <http://localhost:3000>

This is a REST API, so it works using the following HTTP methods:

-   GET (Read): Gets a list of items, or a single item
-   POST (Create): Creates an item
-   PATCH (Update): Updates an item
-   DELETE: Deletes an item

### Creating new models

If you need to add more models to the project just create a new file in `/app/models/` and it will be loaded dynamically.

### Creating new routes

If you need to add more routes to the project just create a new file in `/app/routes/` and it will be loaded dynamically.

### Creating new controllers

When you create a new controller file, try to also create another file with validations. Ex. `countries.js` and `countries.validate.js`. An example of this is included in the repository.

## Developer Informations

### Server init
- To a fast server initialization
```bash
./init-server.sh
```

### Mongo
- To initialize mongdb service
```bash
mongod --dbpath=/usr/local/var/mongodb/db --replSet rs0
```

### Redis

- Flush all data
```bash
/usr/local/bin/redis-cli -p 6379 FLUSHALL
```

#### Redis Command
- Install Command

```bash
npm install -g redis-commander
```
- Run Command
```bash
redis-commander --redis-port 6379 --nosave --open
```
- To access in browser goes to http://localhost:8081/

#### ElasticSearch Command
- Install ElasticSearch

```bash
cd /tmp
#cd config #elasticsearch.yml config port, authentication, etc. 9200
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-16.7.1.tar.gz
tar -zxvf elasticsearch-16.7.1.tar.gz
mv elasticsearch-16.7.1 /usr/local/opt/elasticsearch
cd /usr/local/opt/elasticsearch/
ln -s ../opt/elasticsearch/bin/elasticsearch /usr/local/bin/elasticsearch
ln -s ../opt/elasticsearch/bin/elasticsearch-cli /u sr/local/bin/elasticsearch-cli
ln -s ../opt/elasticsearch/bin/elasticsearch-env /usr/local/bin/elasticsearch-env
cd /usr/local/opt/elasticsearch/
elasticsearch
```
- Run as daemon
```bash
elasticsearch -d -p /usr/local/var/run/elasticsearch.pid
```

- In case of error: Error [cluster_block_exception] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]
  - Go to /usr/local/opt/elasticsearch/data/ and delete the content.
  - Look this links: https://discuss.elastic.co/t/forbidden-12-index-read-only-allow-delete-api/110282/8 | https://selleo.com/til/posts/esrgfyxjee-how-to-fix-elasticsearch-forbidden12index-read-only
  - In Kibana, run the command:
  ```bash
    PUT /_settings
    {
       "index.blocks.read_only_allow_delete": null
    }
  ```
  - By curl command:
  ```bash
  curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}'
  ```

#### Kibana
- To install Kibana

```bash
cd /tmp
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-darwin-x86_64.tar.gz
tar -zxvf kibana-6.5.4-darwin-x86_64.tar.gz
mv kibana-6.5.4-darwin-x86_64 /usr/local/opt/kibana
ln -s ../opt/kibana/bin/kibana /usr/local/bin/kibana
```

- Run Command
```bash
kibana
```
Access on: http://localhost:5601


#### Global Access
- To ngrok
    - Authenticate with ngrok doing
    ```bash
    ngrok authtoken PUT_HERE_THE_NGROK_ACCOUNT_TOKEN
    ```
    - To run the server with a defined domain: (ex: domain is fonttech)
    ```bash
    ./ngrok http 3000 -subdomain=fonttech -bind-tls=true
    ```
    - To run the server with a random domain:
    ```bash
    ./ngrok http 3000 -bind-tls=true
    ```
- To serveo
    - Start the server running the command:
    ```bash
    ssh -o ServerAliveInterval=60 -R font-tech:80:localhost:3000 serveo.net
    ```

---
_Source: https://npm.io/package/fonttech_base_server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
