Creating and Running an Express.js Project
What is Express.js?
Express.js is a fast, open, and minimalist web application framework based on Node.js. It offers a range of powerful features for building single-page, multi-page, and hybrid web applications. Express.js is one of the most popular frameworks in the Node.js ecosystem, loved by developers for its simplicity and flexibility.
Main Features and Advantages of Express.js
- Simplicity and Flexibility: Express.js provides a simple API and a flexible middleware mechanism, making web application development very efficient.
- Middleware: Supports various middleware, allowing easy handling of requests and responses.
- Routing: Powerful routing capabilities to handle requests based on different HTTP methods and URL paths.
- High Integration: Can seamlessly integrate with various template engines, databases, and other Node.js modules.
- Community and Ecosystem: Boasts a large community and extensive support for third-party middleware and plugins.
Using Express.js can help developers quickly build high-performance web applications and APIs.
Creating and Running an Express.js Project with ServBay
In this article, we will use the Node.js environment provided by ServBay to create and run an Express.js project. We will leverage ServBay's "Hosting" feature to set up the web server and use a reverse proxy to access the project.
Creating an Express.js Project
Initialize the Project
First, ensure you have installed the Node.js environment provided by ServBay. Then, use the following commands to initialize a new Express.js project:
bashcd /Applications/ServBay/www npx express-generator servbay-express-app
1
2Install Dependencies
Enter the project directory and install the dependencies:
bashcd servbay-express-app npm install
1
2
Modifying Express.js Project Output
Edit the
routes/index.js
FileOpen the
routes/index.js
file and modify the content to output "Hello ServBay!":javascriptvar express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { res.send('Hello ServBay!'); }); module.exports = router;
1
2
3
4
5
6
7
8
9
Entering Development Mode
Run Development Server
Start the development server and specify the port (e.g.: 8585):
bashPORT=8585 DEBUG=servbay-express-app:* npm start
1This will start a local development server and expose port 8585.
Configure ServBay Host for Reverse Proxy
Use ServBay's "Hosting" feature to access the development server through a reverse proxy. In ServBay's Host settings, add a new reverse proxy:
- Name:
My first Express.js dev site
- Domain:
servbay-express-test.dev
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8585
For detailed setup steps, refer to Adding a Nodejs Development Website.
- Name:
Access Development Mode
Open your browser and visit
https://servbay-express-test.dev
to view the project in real-time. With ServBay's support for custom domains and free SSL certificates, you will benefit from higher security.
Deploying Production Version
Prepare Production Environment
Ensure your project can run properly in a production environment. Typically, Express.js projects do not require special build steps, but you may need to set environment variables or configure other settings.
Run Production Server
Start the production server and specify the port (e.g.: 8586):
bashPORT=8586 NODE_ENV=production npm start
1Configure ServBay Host for Reverse Proxy
Use ServBay's "Hosting" feature to access the production server through a reverse proxy. In ServBay's Host settings, add a new reverse proxy:
- Name:
My first Express.js production site
- Domain:
servbay-express-test.prod
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8586
- Name:
Access Production Mode
Open your browser and visit
https://servbay-express-test.prod
to view the production version. With ServBay's custom domain and free SSL certificate, your website will have higher security and credibility.
Database Connection
ServBay provides support for Redis, MariaDB, PostgreSQL, and MongoDB databases. Here are examples of how to connect to these databases.
Connecting to MongoDB
Install
mongoose
:bashnpm install mongoose
1Then import and connect in your project:
javascriptvar mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/servbay-express-app', { useNewUrlParser: true, useUnifiedTopology: true });
1
2
3Connecting to Redis
Install
redis
:bashnpm install redis
1Then import and connect in your project:
javascriptvar redis = require('redis'); var client = redis.createClient(); client.on('error', function (err) { console.log('Error ' + err); });
1
2
3
4
5
6Connecting to MariaDB
Install
mariadb
:bashnpm install mariadb
1Then import and connect in your project:
javascriptvar mariadb = require('mariadb'); var pool = mariadb.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'servbay_express_app' }); pool.getConnection() .then(conn => { console.log("Connected to MariaDB"); conn.release(); //release to pool }) .catch(err => { console.log("Not connected due to error: " + err); });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16Connecting to PostgreSQL
Install
pg
:bashnpm install pg
1Then import and connect in your project:
javascriptvar { Pool } = require('pg'); var pool = new Pool({ user: 'user', host: 'localhost', database: 'servbay_express_app', password: 'password', port: 5432, }); pool.connect((err, client, done) => { if (err) throw err; console.log('Connected to PostgreSQL'); done(); });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
By following the above steps, you have successfully created and run an Express.js project, used ServBay's features to manage and access your project, and connected to various databases.