Rest Api with node.js localhost not answering

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Rest Api with node.js localhost not answering



Hy there, I'm trying to learn to make a REST API. I have the following code, where I mention that i have no error. When I try to access localhost:3000 nothing happens it's just reloading a blank page. What am I doing wrong?


localhost:3000



Servers.js


const http = require('http');
const app = require('./app').default;
const port = process.env.port || 3000;

const server = http.createServer();

server.listen(port);



App.js


const express = require('express');
const app = express();

app.use((req, res, next) => {
res.status(200).json({
message: 'It works!'

});
});

module.exports = app;




1 Answer
1



You've not defined any route. Express generator generates the boilerplate code which is use full to start with.



You can also define a route like this:


app.use('/', function (req, res, next) {
return res.render('index', {title: 'DASHBOARD', 'data': ''});
});



Have a look at this doc: https://expressjs.com/en/starter/generator.html





Thanks! It works :)
– Mircea
1 hour ago





You're welcome :)
– Zeeshan Tariq
1 hour ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived