简体   繁体   中英

How to use lite-server for nodejs application?

I started a project using lite-server. So far I only used an index.html file and client-side Javascript, but now I want to add server-side JavaScript functionality. How can I configure lite-server to use server.js for routing? Using just node the command would be

node server.js

However I start my server like this:

lite-server

How can I specify that for example if '/someurl' is requested, the url gets resolved using server.js? I assume this can be done in bs-config.json as module.export like

module.exports = {
 server: {
    middleware: {
      // overrides the second middleware default with new settings
      })
    }
  }
};

What might the right definition for my case?

Ok, I have solved it. Instead of using server.js, I have to define bs-config.js to handle the routing. That's my solution using express:

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

app.use(express.static('public'));

app.get('/', function (req, res) {
   res.sendFile( __dirname + "/src/" + "index.html" );
})

app.get('/admin', function (req, res) {
   res.sendFile( __dirname + "/src/" + "admin.html" );
})

app.get('/hello', function (req, res) {
   res.send('hello');
})

module.exports = {
  "server": {
    "baseDir": "src",
    "routes": {
      "/node_modules": "node_modules"
    },
    middleware: {
      1: app,
  },
},
port:3001,
};

By setting app as middleware on position 1, lite-server's default behaviour gets overwritten and requests get handled by express.

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