Remove extension from url in expressjs
Hello viewers, whats up!! Here i am back again with the new series of Nodejs. Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on the V8 JavaScript runtime engine, which is the same engine that powers the Google Chrome browser.
Node.js is commonly used for building APIs (Application Programming Interfaces). You can create RESTful APIs or GraphQL APIs using Node.js to handle HTTP requests and serve data to client applications.
Key features of Node.js include:
# Asynchronous - Node.js is designed to be asynchronous, meaning it can
handle a large number of concurrent connections without the need for
multithreading.
# Single-threaded, Non-blocking I/O -
Node.js uses a single-threaded event loop to manage asynchronous
operations.
# npm (Node Package Manager): npm is the default
package manager for Node.js, allowing developers to easily manage and install
libraries and dependencies for their projects.
# Server-Side Development: Node.js is commonly used for server-side development, where it excels at
handling real-time, data-intensive applications, such as chat applications,
online gaming, and streaming services.
But Why we are using Expressjs Here, just because of incase if
you want to make API using nodejs directly then
its a very complex method to create API and integrate directly thats why we
are using Expressjs framework and in this framework with the help, you
can create RESTful API easily.
Remove extension from url
Why i am sharing this blog. you are all aware about our previous blog
where we have to written Load Separate HTML Files in expressjs but there is
one problem when user visit our .html, .jsx, page. User will easily detect
which technology we have used.
Let's code
const express = require('express') const path = require('path') const app = express(); const publicPath = path.join(__dirname, 'public'); app.get('',(__,resp) => { resp.sendFile(`${publicPath}/home.html`) }) app.get('/about',(__,resp) => { resp.sendFile(`${publicPath}/about.html`) }) app.get('/contact',(__,resp) => { resp.sendFile(`${publicPath}/contact.html`) }) app.listen(5000)
if you people would have seen our previous blog carefully then you will not face any problem in understanding this code. Here is only line of code that is different from previous code.
In our previous blog, we had used static page and this line -
app.use(express.static(publicPath));
But this time we will not use it at this time and we will use only app.get method and this code '/about' define whatever name keep you want except same name as file name or extension and then (__, this blank area define as a request parameter and then response (resp).
After jump into next line we will send directory path or actual file name and extension with the help of resp.sendFile.
# Install redux and saga packages - CLICK HERE
# Make reducer wrapper - CLICK HERE
# Action in reducer - CLICK HERE
# Reducer in redux -
CLICK HERE
# Switch Stmt in redux -
CLICK HERE
# Get data in component from redux - CLICK HERE
# Remove from cart - CLICK HERE
# Add Redux Toolkit in react redux saga - CLICK HERE
# Configure MiddleWare saga - CLICK HERE
# Call API with Saga and Set Result in react redux saga - CLICK HERE
# Product list ui with API data in react redux saga - CLICK HERE
# Remove to Cart with ID react redux saga - CLICK HERE
# Add Routing and Make Cart Page - CLICK HERE
# Show Added To Cart Product with Price Calculation -
CLICK HERE
Disclaimer
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.