简体   繁体   中英

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?

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM