# Welcome

Meet Spring.js, the best way for nodejs developers to create projects fast. Spring.js has easy documentation, lots of features and usability. Created by a nodejs developer, DanCodes.

![](https://491712002-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LmQsTbnN_53C2m6WT2s%2F-LmUw9Kj6aJKP6MJtOxY%2F-LmUwB8dzp_4dQiMU8As%2Fbanner.png?alt=media\&token=170eb37e-10c4-450e-8a25-6d70dab14c09)

![](https://circleci.com/gh/dan-online/spring.js.svg?style=svg\&circle-token=f474a522a9749f7c41056ba4c2f1044cc3f65bf3) ![](https://img.shields.io/badge/dependencies-up%20to%20date-lime.svg) ![](https://api.codacy.com/project/badge/Grade/d6edf679d05f4da183d94b9ffcfc5dff)

![](https://api.travis-ci.org/dan-online/spring.js.svg?branch=master) [![Actions Status](https://github.com/dan-online/spring.js/workflows/GitCI/badge.svg)](https://github.com/dan-online/spring.js/actions)

## Features

* Mongo Database
* Express connection
* Production ready
* Session
* Helmet
* Trust proxy
* Logging
* Body parsing
* Cookie parsing
* CLI
* Optimised

## Install

```bash
$ npm install -g --save js-spring
```

## Basic Use

```javascript
const SpringJS = require("js-spring");
const { app, database } = new SpringJS({ name: "test" });
```

## Documentation

* [Startup](/startup)
  * [Options](/startup#options)
  * [Name](/startup#name)
  * [Port](/startup#port)
  * [Log](/startup#log)
  * [Mongo](/startup#mongo)
  * [Views directory](/startup#views-directory)
  * [Public directory](/startup#public-directory)
* [Express](/express)
* [Database](/database)

## License

(The MIT License)

Copyright (c) 2019 DanCodes <dan@dancodes.online>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# Startup

```javascript
const SpringJS = require("js-spring");
new SpringJS({ name: "test" });
```

## Options

* [Name](/startup#name)
* [Port](/startup#port)
* [Log](/startup#log)
* [Mongo](/startup#mongo)
* [Routes](/startup#routes)
* [Views directory](/startup#views-directory)
* [Public directory](/startup#public-directory)

### Name

* Kind: String
* Example: name: "test"
* Description: This will be used just for logging purposes

```javascript
new SpringJS({ name: "test" });
```

### Port

* Kind: Integer (0-65536)
* Example: port: 8080
* Description: What port to run the web server on

```javascript
new SpringJS({
  name: "test",
  port: 8080
});
```

{% hint style="info" %}
We suggest port 8080 for development but you are free to use any port number between 0 and 65536
{% endhint %}

### Log

* Kind: Boolean
* Example: log: true
* Description: Whether or not to log requests to the console

```javascript
new SpringJS({
  name: "test",
  port: 8080,
  log: true
});
```

{% hint style="success" %}
Logging will show path of request, speed and status code sent
{% endhint %}

### Mongo

* Kind: String
* Example: mongo: "mongodb://localhost:27017/"
* Description: Mongo server to connect to your database

```javascript
new SpringJS({
  name: "test",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/"
});
```

{% hint style="warning" %}
We use enmap and enmap-mongo for connecting and using your mongo database both of which work but are no longer supported.
{% endhint %}

### Routes

* Kind: Array
* Example: routes: \[{ url:'/', router: require('./api.js') }]
* Description: For assigning routes to your application.

```javascript
new SpringJS({
  name: "test",
  port: 8080,
  log: true,
  routes: [
    {
     url:'/api',
     router: require('./api.js')
    },
    {
     url:'/api2',
     router: require('./api2.js')
    }
  ],
  mongo: "mongodb://localhost:27017/"
});
```

### Views Directory

* Kind: String
* Example: viewsDir: "./test/views"
* Description: The views directory

```javascript
new SpringJS({
  name: "test",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/",
  viewsDir: "./test/views"
});
```

{% hint style="info" %}
We recommend this option only if you are setting a view engine like ejs or pug
{% endhint %}

### Public Directory

* Kind: String
* Example: publicDir: "./test/views"
* Description: The public directory for express

```javascript
new SpringJS({
  name: "test",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/",
  viewsDir: "./test/views",
  publicDir: "./test/public"
});
```


# Express

We use express to start a web server and to control it. We also use body parsing, cookie parsing and helmet to keep the server secure and functional so you don't have to.

You can access the express server through the module (1) or on startup in the constructor (2)

```javascript
// 1
const app = require("js-spring").app;

// 2
const SpringJS = require("js-spring");
const { app } new SpringJS({ name: "test" });
```

{% hint style="info" %}
We recommend for multiple files by starting up in a main file and accessing the express app using the first method
{% endhint %}

## Example usage

```javascript
const SpringJS = require("js-spring");
const { app } new SpringJS({ name: "test" });

app.use(function(req, res, next) {
    req.session.name = "DanCodes";
    console.log("Middleware rocks! just like Spring.js");
    next();
});

app.get("/", function(req, res) {
    res.status(200).send("I'm awake!");
});

app.post("/body", function(req, res) {
    res.status(200).send(req.body);
});

app.get("/session", function(req, res) {
    res.status(200).send(req.session.name)
});
```


# Database

We use enmap to connect to your mongo database as it is fast and easy to use. Because of deprecation we use enmap 3 and enmap-mongo.

All commands can be found here: <https://enmap.evie.dev/v/3/>

You can access the database through the module (1) or on startup in the constructor (2)

```javascript
// 1
const database = require("js-spring").database;

// 2
const { database } new SpringJS({ name: "test" });
```

{% hint style="info" %}
We recommend for multiple files by starting up in a main file and accessing the database using the first method
{% endhint %}

## Example usage

```javascript
const SpringJS = require("js-spring");
const { database } new SpringJS({ name: "test" });

database.set("DanCodes", "Spring.js");

console.log("DanCodes wrote " + database.get("DanCodes") + "!");
```


# Examples

## Advanced example

```javascript
const SpringJS = require("js-spring");
const { app, database } = new SpringJS({
  name: "advanced",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/",
  viewsDir: "./views",
  publicDir: "./public"
});

app.get("/", function(req, res) {
  database.set("Startup", new Date());
  res.send("You visited at " + database.get("Startup"));
});
```

## EJS

```javascript
const SpringJS = require("js-spring");
const { app, database } = new SpringJS({
  name: "advanced",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/",
  viewsDir: "./views",
  publicDir: "./public"
});

app.set("view engine", "ejs");

app.get("/", function(req, res) {
  res.render("index", { time: new Date() });
});
```

## Session

```javascript
const SpringJS = require("js-spring");
const { app, database } = new SpringJS({
  name: "advanced",
  port: 8080,
  log: true,
  mongo: "mongodb://localhost:27017/",
  viewsDir: "./views",
  publicDir: "./public"
});

app.set("view engine", "ejs");
app.use(function(req, res, next) {
  req.session.username = "Tester";
  next();
});

app.get("/", function(req, res) {
  res.render("index", { name: req.session.username });
});
```


# Command Line

The integrated cli built into spring.js is to generate a new spring project built for you.

```bash
$ npm i -g js-spring
$ spring help
```

{% hint style="info" %}
This feature is still in beta and being worked on now. If you have any suggestions we recommend opening a issue in [github](https://github.com/dan-online/spring.js/issues)
{% endhint %}


