Basic Node and Express - Serve JSON on a Specific Route

I’ve tried everything possible but it keeps showing not found whenever I try the /json in my web view. I even added a cors but it still isn’t working

import express from 'express';
import cors from 'cors';

const app = express();

app.use(cors());

app.get('/json', (req, res) => {
  return res.status(200).json({"message": "Hello json"});
});

Your project link(s)

solution: boilerplate-npm - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76

Challenge Information:

Basic Node and Express - Serve JSON on a Specific Route

const express = require(‘express’);

example of a very basic route

1 Like

Alright, you just need one file index.js for your express. Include your code and

app.listen(3000, () => { // Make your API live
  console.log('Express server initialized');
});

And the package.json with the express package. Replit provides you a template with the prepared setup. What you need to do is to add the cors and change the routes.

Hope this helps.

3 Likes

This worked; however, I had to change the port number. Thanks

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.