-
Notifications
You must be signed in to change notification settings - Fork 0
Home
A simple , clean , minimalist web framework for Node.js.
Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory.
$ mkdir myapp
$ cd myapp
Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling.
$ npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them. But in entry point or the main file enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js
, hit ENTER/RETURN to accept the suggested default file name.
Now install Simple Express in the myapp directory .For example:
$ npm install @sanmeet007/simple-express
You can use the newer module syntax with imports in your app , All you need to do is set
type
in package.json file tomodule
.
A minimal Simple Express hello world
application looks something like this:
import express from "@sanmeet007/simple-express";
const app = express();
app.get("/" , (req,res) =>{
return res.send("Hello world");
});
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
Each route can have one or more handler functions, which are executed when the route is matched.
Route definition takes the following structure:
app.METHOD(PATH, HANDLER)
Where:
- app is an instance of simple express server.
- METHOD is an HTTP request method, in lowercase.
- PATH is a path on the server.
- HANDLER is the function executed when the route is matched.
The following examples illustrate defining simple routes.
Respond with Hello World! on the homepage:
app.get('/', (req, res) => {
return res.send("Hello World!");
})
Respond to POST request on the root route (/), the application’s home page:
app.post('/', (req, res) => {
return res.send("Request method POST");
})
In addition to the POST/GET routing methods to accept any other method ( get/post also ) like PUT
or DELETE
. We use the app.route
method.
It has the following structure :
app.ROUTE(PATH , METHOD, HANDLER)
Respond to a PUT request to the /user route:
app.route("/user" , "PUT", (req, res) => {
return res.send("Got a PUT request at /user")
})
In app.route we have an additional argument
METHOD
which describes method to invoke whenreq.method
matches theMETHOD
.
To serve static files such as images, CSS files, and JavaScript files, use the app.setStatic()
built-in function.
The function signature is:
app.setStatic("public");
Now, you can load the files that are in the public directory:
http://localhost:3000/assets/script.js
http://localhost:3000/assets/style.css
http://localhost:3000/index.html
The
index.html
file needs to have extension. The server wont't serveindex.html
for any get request to any public folder like/folder/
.
For serving custom error pages instead of the default browser error page you need to error handlers .
Error definition takes the following structure:
app.error(ERROR_CODE, HANDLER)
Where:
- app is an instance of simple express server.
- ERROR_CODE is the Http error code through which the router decides which error method to call.
- HANDLER is the function executed when the route is matched.
- The handler needs to return the
Response
which needs to have the specific error status code.
This method needs to be used carefully
The following examples illustrate defining simple error handlers.
Responding to Internal server error:
app.error(500, (req, res) => {
return res.send("Internal server error !" , 404);
});
Responding to famous Not found or 404 error:
app.error(404, (req, res) => {
return res.send("Page not found !");
});
- Response & Request Methods + Properties
- Middlewares ( Intercepts the req and provides checks by the developer)
- Rendering Templates ( EJS )
- File uploads ( Busboy )
- API Building ( Returning JSON )
Please feel free to contact if any queries or improvement tips
Email : ssanmeet123@gmail.com