Create a simple Node Server Skeleton.

Introduction

This tutorial is quite useful for quick start for many simple projects which doesn’t need complex functionalities or projects which are for tutorial purpose.

If you want to build a more complex Node Server with express, routers…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Luckey

Introduction

This tutorial is quite useful for quick start for many simple projects which doesn't need complex functionalities or projects which are for tutorial purpose.

If you want to build a more complex Node Server with express, routers, controllers, databases, Redis and etc. You can ahead to my another article.

Code

Set up environment

First, you would need to install the necessary dependencies. In your terminal, navigate to the root directory of your project and run the following command to initialize packages:

npm init

Install express and necessary packages with npm

npm install express cors dotenv

or install packages with yarn

yarn add express cors dotenv

If you are using TypeScript, you must also install the following packages

npm install @types/express @types/cors

or install packages with yarn

yarn add @types/express @types/cors

Server.js (JavaScript)

Once express is installed, you can create the server file. In your project directory, create a file called server.js and add the following code:

import express from "express";
import cors from 'cors';
import dotenv from "dotenv";

// Get environment variables
dotenv.config()

// Create the express server and configure it to use json
const app = express();
app.use(express.json());

// Configure cors policy
app.use(cors())

// Set up a API call with GET method
app.get('/data', (req, res) => {
  // Return some sample data as the response
  res.json({
    message: 'Hello, world!'
  });
});

// Start the server on port configured in .env (recommend port 8000)
app.listen(process.env.PORT, () => {
  console.log(`SERVER IS RUNNING AT PORT ${process.env.PORT}`);
});

Server.ts (TypeScript)

the only different between Server.js and Server.ts is showed below:

// You need to classify the type of variable
const app: Application = express()

Common Errors

SyntaxError: Cannot use import statement outside a module
Solution: Add below line to your Package.json

"type": "module",

Conclusion

In conclusion, creating a simple Node server skeleton is a straightforward process that involves setting up a new Node project, installing the necessary dependencies, and configuring the basic server structure.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Luckey


Print Share Comment Cite Upload Translate Updates
APA

Luckey | Sciencx (2022-12-16T00:15:50+00:00) Create a simple Node Server Skeleton.. Retrieved from https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/

MLA
" » Create a simple Node Server Skeleton.." Luckey | Sciencx - Friday December 16, 2022, https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/
HARVARD
Luckey | Sciencx Friday December 16, 2022 » Create a simple Node Server Skeleton.., viewed ,<https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/>
VANCOUVER
Luckey | Sciencx - » Create a simple Node Server Skeleton.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/
CHICAGO
" » Create a simple Node Server Skeleton.." Luckey | Sciencx - Accessed . https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/
IEEE
" » Create a simple Node Server Skeleton.." Luckey | Sciencx [Online]. Available: https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/. [Accessed: ]
rf:citation
» Create a simple Node Server Skeleton. | Luckey | Sciencx | https://www.scien.cx/2022/12/16/create-a-simple-node-server-skeleton/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.