Express
Make sure you have the express
package installed in your project. If you don't have it installed, you can install it using the following command:
npm install express
How to use the Snippet
Write the snippet in your new file expsrv
:
server.js
import express from 'express';
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.use((req, res) => {
res.status(404).send('404 Not Found');
});
app.listen(PORT, () => {
console.log('Server is running on port: ', PORT);
});
In the snippet above, we created a simple Express.js server that listens on port 3000. The server has two routes:
- The root route
/
that returns aHello World!
message. - A 404 route that returns a
404 Not Found
message for all other routes.
How to use the Command
- Open the command palette
(Ctrl+Shift+P
orCmd+Shift+P on Mac)
. - Type
MERN: Express Function
and pressEnter
. - Select the function to create (
Server
,Router
) and enter the file name. - Press
Enter
and the snippet will be created.
Show GIF
“You can make use of any with the right approach.” – Askeladd (Vinland Saga)